How do you know about the maximum range of elements that can be stored in an array?

While I was creating an array with size equal to Integer .MAX_VALUE

public static void main(String[] args) {
        int[] array = new int[Integer.MAX_VALUE]; // This gives an Error
}

      

I got this error:

Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds
VM limit at com.arrays.TimeArray2.main(TimeArray2.java:6)

      

Until now, I knew that an array in Java can, at most, store up to 2,147,483,647 or 2 ^ 31 values.

So I searched Google for this reason and found this question on stackoverflow.com: Do Java arrays have a maximum size? ...

The accepted answer to this discussion says:

In a recent HotSpot VM, the correct answer is Integer.MAX_VALUE - 5

      

Another popular answer:

Some VMs reserve some header words in an array. The maximum "safe" number would be
2,147,483,639 (Integer.MAX_VALUE - 8). Attempts to allocate larger arrays may 
result in java.lang.OutOfMemoryError.

    If you have the source code for the java classes, checkout
        java.util.ArrayList.class (line 190):

      

But the point is, none of the above is true (at least not in my case). When I run the program with the range as one of the above two values, I still get the same error.

Not only that, the error appears even with the following set of values:

int[] array = new int[Integer.MAX_VALUE-10];   // Error
int[] array = new int[Integer.MAX_VALUE-100];  // Error
int[] array = new int[Integer.MAX_VALUE-1000]; // Error 
int[] array = new int[2147483647];             // Error
int[] array = new int[214748364];              // Error

      

So finally my question is:

1) What is max. not. of elements an array can store in Java?

2) How to make sure it will work on all platforms (or multiple JVM implementations) satisfying the popular Java Write Once Run Anywhere binding ?

+3


source to share


2 answers


Java has a limit on the maximum size of an array that your program can allocate. The exact limit is platform specific, but typically somewhere between 1 billion and 2.1 billion items.

Reason for java.lang.OutOfMemoryError



The error is generated by native code inside the JVM. Occurs before allocating memory for an array when the JVM performs a platform-specific check: regardless of whether the distributed data structure is addressable on that platform.

More details .

+1


source


2 ^ 31 * sizeof (integer) = 2 ^ 31 * 4 = 2 ^ 33

the maximum addressable memory for 32 bit m / s is 2 ^ 32-1. so why is this expected?

you have to remember that usually user memory is half 4GB (and depends on the platform)

try with array size <user space address memory (condition of your max generation value) that might work .. and that should be the max allowed limit.



So finally my question is:

1) What is max. not. of elements an array can store in Java? several bytes less than the allocated heap space. will depend on Xmx, Xmn, new Ratio

2) How to be sure that it will work on all platforms (or multiple JVM implementations) satisfying the popular Java Write Once Run Anywhere tagline?

config will give the same result on most platforms. as popular array implementation is 2 + 1 header + array word.

0


source







All Articles