views:

811

answers:

4

I used the Out Of Memory help from sun's site. Where it is quoted as

Out Of Memory : Requested array size exceeds VM limit

This indicates that the application attempted to allocate an array that is larger than the heap size. For example, if an application tries to allocate an array of 512MB but the maximum heap size is 256MB, then this error will be thrown. In most cases the problem is likely to be either that the heap size is too small or that a bug results in the application attempting to create an array whose size is calculated to be incorrectly huge.

I tried to simulate this by

import java.util.ArrayList;
import java.util.List;

public class JavaTest {
    public static void main(String[] args){
     long[] ll = new long[64*1024*1024];
    }
}

on my machine with

javac *.java;java -Xmx256m JavaTest

. But the above line is producing

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at JavaTest.main(JavaTest.java:7)

What am I missing?

Update : My java version is

$java -version
java version "1.6.0_15"
Java(TM) SE Runtime Environment (build 1.6.0_15-b03)
Java HotSpot(TM) Server VM (build 14.1-b02, mixed mode)
A: 

Try allocating, say, an ArrayList and to double its size run-time.

lorenzog
This again giving me like Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:2760) at java.util.Arrays.copyOf(Arrays.java:2734) at java.util.ArrayList.ensureCapacity(ArrayList.java:167) at java.util.ArrayList.add(ArrayList.java:351) at JavaTest.main(JavaTest.java:27)
DKSRathore
A: 

The array is allocated as continous heap space and memory can get fragmented due to object allocaion and garbage collection. The out of memory error occurs due to lack of a solid block of memory to allocate the array. Did you sent the JVM memory explicitly with -Xmx flag ?

whatnick
Yes I did. javac *.java;java -Xmx1000m JavaTest
DKSRathore
+4  A: 

For me

long[] l = new long[Integer.MAX_VALUE];

yields Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit

 

PS: if you need to produce the exception for the purpose of testing, you could also just throw new OutOfMemoryError("Requested array size exceeds VM limit") yourself.

Adrian
Thanks. This is working for me too. But why didn't my code worked earlier?
DKSRathore
Ok. So my system is producing this exception in the range of 1024*1024*1024-2 to Integer.MAX_VALUE
DKSRathore
@Adrian, How do I know my VM limit?
DKSRathore
I dont know either, but `Integer.MAX_VALUE` should be a safe guess for test (I assume you need to provoke that for a test), aint it? *(Actually if you need it for testing, you could throw the Exception yourself, right?)*
Adrian
But still why this exception and when will occur is not clear to me?
DKSRathore
To me neither, my guess is that each VM has both an upper limit for the heap size *and* an upper limit for the length of an array (independent of its size in bytes). But that is a guess only.
Adrian
Here be dragons!long[] l = new long[2147483645]; exceptions message reads - Requested array size exceeds VM limitlong[] l = new long[2147483644]; exceptions message reads - Java heap space errorsInteger.MAX_VALUE = 2147483647 for reference.
Gordon
@Gorden, is your analysis valid on all systems? How much jvm memory do you have?
DKSRathore
I'm just using the default memory allocation. Runtime.getRuntime().totalMemory() is 85393408 bytes, 85mb
Gordon
A: 

Just looking at example fo people have the problem it seems to occur when people are using the java native interface, bridge between C++ applications.

When running pure java application the standard procedure seems for allocating an array is calculate the bytes needed from the size variable passed in the array deceleration throwing an java.lang.OutOfMemoryError: Java heap space exception.

When the array is created outside the JVM it looks it generates this other exception. I'm sure there is some technical reason for this different behavior.

You could try replicating the problem this person describes on this other forum.

Gordon
Thanks for the answer. But the forum link as well is not telling why this is occurring? Is VM limit error is more concerned with Native memory that VM memory?
DKSRathore