Java application "java.lang.OutOfMemoryError: Java heap space"

Solution Verified - Updated

Environment

  • Java (all vendors)

Issue

  • JBoss Application server stops accepting incoming requests.

  • The following message appears in the JBoss server.log  file :

    ERROR [STDERR] java.lang.OutOfMemoryError: Java heap space 
    
  • The following log message appears in the JBoss Operations Network (JON) server log:

    Caused by: java.lang.OutOfMemoryError: Java heap space
    ERROR [STDERR] Exception in thread "RMI TCP Connection(44627)-127.0.0.1"
    ERROR [STDERR] java.lang.OutOfMemoryError: Java heap space
    
  • We would like to root cause analysis of the heap dump generated by one of our application (GRCC) and would like to get some recommendation on heap size parameter setting.

  • Application crashes 2 times a day due to heap space full and have to be restarted

Resolution

If the heap is too small to support the application and use case, increase the maximum heap size with the -Xmx JVM option.

If there is unintended object retention, update the code to eliminate or decrease the retention.

After an OutOfMemoryError condition, the JVM can be in an unknown or unreliable state. Even if the JVM appears to be operating normally, it is recommended that you restart the Java application to ensure a clean environment. Applying KB45882 is useful to avoid the JVM going into unreliable state.

The JVM option "-XX:+HeapDumpOnOutOfMemoryError" generates a heap dump when an OOME occurs.

Root Cause

This error is caused by an undersized heap for the application's requirements or unintended object retention:

Diagnostic Steps

  • What OS and version? 32 or 64 bit?

  • What JVM and version? 32 or 64 bit?

  • How much RAM?

  • Is there available RAM to increase the heap size if  necessary?

    • On Linux, obtain meminfo output when the Java application is running:
    # cat /proc/meminfo > meminfo.txt
    
  • What JVM options are being used?

    • On JBoss EAP, options are output to the beginning of the console log or boot.log (EAP 4.2 CP07 and 4.3 CP06 or later).  Verify the desired heap settings are being properly found and applied.
  • Is  this a newly deployed application, or has it been in production for  some time and only recently this issue has appeared.

  • Are there any particular circumstances surrounding when the  OutOfMemoryError happens? For example, does it happen when specific  functionality is executed (e.g. a file upload, a batch job, etc.), after a certain amount of time, under high load, etc.

  • Get a heap dump (How do I create a Java heap dump?) when the issue happens and analyze it (How do I analyze a Java heap dump?) to see where the retention points are.

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.