JVM exceeds maximum memory defined with -Xmx

15,027

Solution 1

So how could a JVM with 3GB heap, 256MB permgen, and even some overhead consume 6.9GB?

Possible explanations include:

  • lots and lots of thread stacks,
  • memory-mapped files that are not being closed when they should be,
  • some native code library using (possibly leaking) out-of-heap memory.

I would be inclined to blame the application before blaming the JVM.

Solution 2

So long story short, my initial reaction was correct it was a bug in the JVM. We were using 1.6.0_21 and it turns out that we were experiencing the exact same error as outlined in https://confluence.atlassian.com/pages/viewpage.action?pageId=219023686. Upgrading to 1.6.0_37 fixed the problem and we went from daily crashes to 2 weeks without a crash.

So while the sentiment to not just blame the JVM is a good policy it seems that one should also be advised not to always assume the JVM is bug free, it like all software has the occasional bug. Plus, seems good policy to keep things up to date.

Thanks for all the help on this one!

Solution 3

http://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/geninfo/diagnos/garbage_collect.html

Note that the JVM uses more memory than just the heap. For example Java methods, thread stacks and native handles are allocated in memory separate from the heap, as well as JVM internal data structures.

So if you have a lot of threads and a lot of native handles, the memory can exceed the heap limit. Are you sure this didn't happen before as well?

Also check out this: Java using more memory than the allocated memory

Share:
15,027

Related videos on Youtube

Benny the Guard
Author by

Benny the Guard

Updated on September 15, 2022

Comments

  • Benny the Guard
    Benny the Guard over 1 year

    We have a Java webapp that we upgraded from Java 1.5.0.19 to Java 1.6.0.21

    /usr/java/jdk1.6.0_21/bin/java -server -Xms2000m -Xmx3000m -XX:MaxPermSize=256m -Djava.awt.headless=true -Dwg.environment=production -Djava.io.tmpdir=/var/cache/jetty -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=31377 -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=false -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/webapp -Dprogram.name=run.sh -Djava.endorsed.dirs=/opt/3p/jboss/lib/endorsed -classpath /opt/3p/jboss/bin/run.jar:/usr/java/jdk1.6.0_21/lib/tools.jar org.jboss.Main -c default
    

    As you can see it should preallocate 2GB of heap and max out at 3GB (why we preallocate so much is because this app is ancient and poorly designed so has a ton of things to load up). The issue we have seen recently after upgrading to the 1.6 is that on occasion memory goes through the roof. While memory usage is likely an app issue the JVM is exceeding the 3GB max setup for heap. Using top I see:

     PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND    
    8449 apache    18   0 19.6g 6.9g 5648 S  4.0 84.8  80:42.27 java             
    

    So how could a JVM with 3GB heap, 256MB permgen, and even some overhead consume 6.9GB? Bug in the JVM that would be fixed by upgrading to build #35? Something missing on what in java could be using the extra memory? Just trying to see if anyone has seen this before.

    • parsifal
      parsifal
      Since you're on Linix, use pmap to find out where the memory is actually going.
  • Benny the Guard
    Benny the Guard over 11 years
    It did not happen before the 1.6 upgrade. However, there were other issues and other app changes as well. So I suspect there is indeed and app error and we are going to try and look at the heap dump we just got. While I understand there could be some extra overhead, > 3 GB of overhead seems excessive. Could threads, headless, etc really use that much?
  • Peter Ilfrich
    Peter Ilfrich over 11 years
    This depends on the threads and especially their call stacks. If you use a lot of recursion in a lot of threads this might very well add up to a lot of memory (which can't be cleared by the GC)
  • Stephen C
    Stephen C over 4 years
    Yes. The importance of keeping your JVM installs up to date. You were having problems using a Java install that was ~2 years (and 7 releases with security patches!) out of date.