While trying to discover the max size of a Java String array on my machine I ran into some interesting results, here is the code,
String [] max;
int i = 15444000;
while(true){
try{
max = new String[i];
System.gc();
Thread.sleep(10);
}catch(InterruptedException e){}
i += 1;
System.out.println(i);
}
Whenever I run this code the output makes it 15444038 before throwing OutOfMemoryError. This leads me to think that the max size of a Java String array on my machine is 15444038, however if i replace
int i = 15444000;
with
int i = 15444037; // or any i between 15444037 and 15444002
The OutOfMemoryError occurs instantly. Why is this, and what is the true max size of a Java String array on my machine?