Simple program allocating 1 multi-dimentional array causes continous GCs (all garbage collectors) if heap is big enough to to create outermost array. It appears that GC is attempted many times. With product build and 6G heap, allocation takes 3 minutes.
With fast debug build, allocation takes more than 20 minutes.
Allocating 1-dimensional array which occupies same memory is fast and does not throw any out of memory exception Why ?
public class Test {
public static void main(String[] args) {
int a1 = 1;
long maxMemory = Runtime.getRuntime().maxMemory();
int s = (int) (Math.sqrt(Math.sqrt((double) maxMemory)));
System.out.println("s: " + s);
int[][][][] a2;
try {
a2 = new int [s][s][s][s];
a2 [s-1][s-1][s-1][s-1] = a1;
if ( a2 [s-1][s-1][s-1][s-1] != 1 ) {
throw new RuntimeException("Error: " + a2 [s-1][s-1][s-1][s-1]);
}
} catch (OutOfMemoryError e) {
System.out.println("Passed.");
}
}
}