[java] Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

I have written a code and I run it a lot but suddenly I got an OutOfMemoryError:

  Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at javax.media.j3d.BoundingBox.<init>(BoundingBox.java:86)
        at javax.media.j3d.NodeRetained.<init>(NodeRetained.java:198)
        at javax.media.j3d.LeafRetained.<init>(LeafRetained.java:40)
        at javax.media.j3d.LightRetained.<init>(LightRetained.java:44)
        at javax.media.j3d.DirectionalLightRetained.<init>(DirectionalLightRetained.java:50)
        at javax.media.j3d.DirectionalLight.createRetained(DirectionalLight.java:116)
        at javax.media.j3d.SceneGraphObject.<init>(SceneGraphObject.java:119)
        at javax.media.j3d.Node.<init>(Node.java:178)
        at javax.media.j3d.Leaf.<init>(Leaf.java:50)
        at javax.media.j3d.Light.<init>(Light.java:270)
        at javax.media.j3d.DirectionalLight.<init>(DirectionalLight.java:87)

This question is related to java out-of-memory

The answer is


enter image description here -Xmx1024m -XX:MaxPermSize=512m -Xms512m

Add this parameter as argument in your server params


I don't know about javax.media.j3d, so I might be mistaken, but you usually want to investigate whether there is a memory leak. Well, as others note, if it was 64MB and you are doing something with 3d, maybe it's obviously too small...

But if I were you, I'll set up a profiler or visualvm, and let your application run for extended time (days, weeks...). Then look at the heap allocation history, and make sure it's not a memory leak.

If you use a profiler, like JProfiler or the one that comes with NetBeans IDE etc., you can see what object is being accumulating, and then track down what's going on.. Well, almost always something is incorrectly not removed from a collection...


You're out of memory. Try adding -Xmx256m to your java command line. The 256m is the amount of memory to give to the JVM (256 megabytes). It usually defaults to 64m.


If you're recompiling a disassembled APK with APK tool:

Just Set Memory Allocation a little bigger

set switch -Xmx1024mto -Xmx2048m

java -Xmx2048m -jar signapk.jar -w testkey.x509.pem testkey.pk8 "%APKOUT%" "%SIGNED%"

you're good to go.. :)


There is a another best/effective way to solve this error,

for example, let's take a loop which counts till 10 thousand, here you may get the error Out of memory, do to solve it you can give the computer time to recover.

So, you can sleep for 400-500ms before you're loop counts the next number :

new Thread(new Runnable() {
            public void run() {
                try {
                    sleep(550); // 550 ms (milli seconds)
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

By doing this, will make you're program slower but you don't get any error till the heap space is full again, so by waiting some ms, you can prevent that error.

You can apply this method other than loop.

Hope it helped you, :D


Well, it's fairly self-explanatory: you've run out of memory.

You may want to try starting it with more memory, using the -Xmx flag, e.g.

java -Xmx2048m [whatever you'd have written before]

This will use up to 2 gigs of memory.

See the non-standard options list for more details.