Our tomcat server threw out "java.lang.OutOfMemoryError: Java heap space", but the heap size in dump file is only 1.7GB, and the -Xmx is 4GB.
I'm not sure what's happened, could you help me?
Environment: Redhat tomcat6 jdk6
XmxSIZE can be greater then Java core dump because
OutOfMemory happen when you try to allocate a large object!
Our application with
-Xmx1000m and 670 MiB dump crashed by
OutOfMemory on uploading 300 MiB attachment to user.
We discover this in VisualVM in "Show Threads" trace on
System.arrayCopy. Reference from local stack frame variable point to 300 MiB byte array.
now we limit attachment size and save ou application from future crashes.
The Salah reply is wrong at so many levels I would consider this outright harmful.
First, the JVM will not trigger Stop The World GC and clean out heap just before dying with OutOfMemoryError. The reason for OOM is that the GC has failed to clean the memory, usually during tens and hundreds of runs prior the OOM.
Second, the recommended ratios are not actually recommended by JVM vendors. The best ratios are heavily application dependent, so without knowing what you are doing you are better off keeping the defaults than following this advice.
First you need to take a look at the following memory parameters, it may not prevent the
OutOfMemoryError, but you should learn about them:
-Xmx2048m -> this param to set the max memory that the JVM can allocate
-Xms1024m -> the init memory that JVM will allocate on the start up
-XX:MaxPermSize=512M -> this for the max Permanent Generation memory
Also and you may want to check this parameters also, those parameters will speed up the
GC activities on your Young space, so the collection will be better.
-XX:MaxNewSize= -> this need to be 40% from your Xmx value
-XX:NewSize=614m -> this need to be 40% from your Xmx value
also you may tell you
JVM what type of
GC to use like (i think this type is already enabled in the newest
JVM versions) :
OutOfMemoryError occured, the
JVM will trigger the
Stop World GC, so it will collect your object and the un referenced object, that why the heap size is less than you
Usually in this case, if you analyzed this heap using one of the analyzing tools, you will clearly find the big objects that filled your memory, that will led you to the code and it will be easy to fix it.