3

While I was creating an array with size equals 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)

Till now I Knew that an array in Java can, at the most, store up to 2,147,483,647 OR 2^31 values.

So I looked up on the Google for the reason behind this and found this Question on stackoverflow.com: Do Java arrays have a maximum size?.

The accepted answer over that discussion says :

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

Another popular answer states as :

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 thing is none of the above is true (at least, not in my case). When i run the program with range as either of the above two values, I still kept getting the same Error.

Not only this, the error pops up 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 are :

1) What is the max. no. of elements which an array can store in Java ?

2) How to be sure that it is going to work on all the platforms (Or Multiple JVM implementations) satisfying the popular Java tagline Write Once Run Anywhere?

Community
  • 1
  • 1
Amitesh Rai
  • 866
  • 11
  • 21
  • @Ruchira Gayan Ranaweera, I think I have given the link for that question above and i was not satisfied by the answer. That's why I asked it here. – Amitesh Rai Dec 11 '14 at 12:05
  • You don't have enough physical memory to create an array that big. At least the JVM has doesn't have enough access. – arynaq Dec 11 '14 at 12:05
  • @arynaq, Perhaps this is one of the criticism which Java received. – Amitesh Rai Dec 11 '14 at 12:12

2 Answers2

1

Java has got a limit on the maximum array size your program can allocate. The exact limit is platform-specific but is generally somewhere between 1 and 2.1 billion elements.

Cause of java.lang.OutOfMemoryError

The error is thrown by the native code within the JVM. It happens before allocating memory for an array when JVM performs a platform-specific check: whether the allocated data structure is addressable in this platform.

Read more.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • I know the causes of java.lang.OutOfMemoryError. What about the class file compiled successfuly on one JVM and executed on some other JVM. Even for experimental purpose, can you be sure that it is going to be executed for sure ? – Amitesh Rai Dec 11 '14 at 12:22
  • 1
    I would guess "no" ... the `-5` and `-8` limits are about internal implementation, but what you're running into are environmental limits of how much memory the JVM has been assigned. Since you have no control over this, I guess all you can do is catch such errors and either refuse to run (if you absolutely _must_ have this size array) or adapt accordingly (process something in smaller chunks). – TripeHound Dec 11 '14 at 12:33
0

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

max addressable memory for 32 bit m/c is 2^32-1. so why is this expected to work.?

you should remember that normally user memory is half of 4GB (and depends on platform)

so try with array size < user space addressable memory(condition your maximum tenured generation value), that may work.. and that should be maximum allowed limit.

So, finally my question are :

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

2) How to be sure that it is going to work on all the platforms (Or Multiple JVM implementations) satisfying the popular Java tagline Write Once Run Anywhere?

a configuration will give same result in most platfrom. as popular array implementation is 2 + 1 word header + array.

Jamsheed
  • 47
  • 3