I was reading Unveiling the java.lang.Out OfMemoryError and I was wondering if I’ve understood it correctly. Is it true that if the Java VM throws a
java.lang.OutOfMemoryError: Requested array size exceeds VM limit
It means that the VM has rejected the creation of an array because it has exceeded a pre-defined limit (exceeded the likes of the VM), and not because I have run out of memory in the heap space?
Am I right to say that java.lang.OutOfMemoryError: Requested array size exceeds VM limit does not indicate a lack of memory?
Even if I have unlimited memory everywhere, the Java VM could still throw a java.lang.OutOfMemoryError: Requested array size exceeds VM limit if it doesn’t like my request to create an array of n size?
Quoting Troubleshooting Guide for Java SE 6 with HotSpot VM
UPDATE: encouraged by the @Pacerier I did some quick testing. I wrote a sample program:
And run it with the following JVM options:
This is what they mean:
-Xms192m -Xmx192m)-XX:NewRatio=2)-XX:SurvivorRatio=6)While testing I discovered the following:
OutOfMemoryError: Java heap spaceThis is the bottom line – you cannot create an object with size larger than old generation. Trying to do so will result in
OutOfMemoryError: Java heap spaceerror. So when can we expect dreadfulRequested array size exceeds VM limit?I tried running the same program with much larger objects. Hitting the array length limit I switched to
long[]and could easily go up to 7 GiB. With 128 MiB of max heap declared the JVM was still throwingOutOfMemoryError: Java heap space(!) I managed to triggerOutOfMemoryError: Requested array size exceeds VM limiterror trying to allocate 8 GiB in a single object. I tested this on a 32-bit Linux computer with 3 GiB of physical memory and 1 GiB of swap.That being said you should probably never hit this error. The documentation seems inaccurate/outdated, but it is true in one conclusion: this is probably a bug since creating such huge arrays is very uncommon.