Tuesday, August 05, 2008

maximum index of an array

Space for
200,000,000 integers : 800MB
256,000,000 integers : 1024MB
Integer.MAX_VALUE (2,147,483,647) : ~8600MB
Integer.MAX_VALUE/2 (1,073,741,823) : ~4300MB

//arrayindextest.java
class arrayindextest {
    public static void main(String[] args) {
        int[] a = new int[256000000]; //or 200000000
        System.out.println(a.length);
    }
}

-Xmx set max heap size flag
-Xms set min heap size flag

200,000,000 integers : 800MB
With new int[200000000]:
java -Xms32m -Xmx1024m arrayindextest
200000000

256,000,000 integers : 1024MB
With new int[256000000]:
java -Xms32m -Xmx1024m arrayindextest
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

Integer.MAX_VALUE (2,147,483,647) : ~8600MB
With new int[Integer.MAX_VALUE]:
Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit

Integer.MAX_VALUE/2 (1,073,741,823) : ~4300MB
With new int[Integer.MAX_VALUE/2]:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

If setting the max heap size to 2048MB:
java -Xms32m -Xmx2048m arrayindextest
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.


Since array index is int (or anything that can be converted to int) and not long, one should be able to create an array of Integer.MAX_VALUE size IF one can "reserve enough space for object heap".

No comments:

Post a Comment