Knowledge Base

Recommendations for recovery upon Out Of Memory error

It is possible to configure the JVM (Java Virtual Machine) such that upon encountering an OOM (Out-Of-Memory) error it will force an exception and crash or simply shut down the application. Taking action upon first OOM occurrence can help in avoiding further victims of low memory situations that may encounter OOM depending on their memory demand at the time and also reduce the time to recovery. Additionally, this may actually allow the failed operations due to OOM to complete successfully after restarting the application and retry of such operations since the memory footprint has changed. Choosing CrashOnOutOfMemoryError over ExitOnOutOfMemoryError is preferred if diagnosing the issue is a priority.

CrashOnOutOfMemoryError

If this option is enabled, when an out-of-memory error occurs, the JVM crashes and produces text and binary crash files (if core files are enabled).

Format
-XX:+|-CrashOnOutOfMemoryError
Example
java -XX:+CrashOnOutOfMemoryError
Neo4j configuration file (neo4j.conf) entry:
dbms.jvm.additional=-XX:+CrashOnOutOfMemoryError

ExitOnOutOfMemoryError

When you enable this option, the JVM exits on the first occurrence of an out-of-memory error. It can be used if you prefer restarting an instance of the JVM rather than handling out of memory errors.

Format
-XX:+|-ExitOnOutOfMemoryError
Neo4j configuration file (neo4j.conf) entry:
dbms.jvm.additional=-XX:+ExitOnOutOfMemoryError

-XX:+ExitOnOutOfMemoryError takes precedence over -XX:+CrashOnOutOfMemoryError.

The ExitOnOutOfMemoryError and CrashOnOutOfMemoryError flags were added to Java SE 8 Update 92. For more information, see the 'New JVM Options' section of the 8u92 Update Release Notes.

JDK-8155004 : CrashOnOutOfMemoryError doesn’t work for OOM caused by inability to create threads

There are many types of OutOfMemoryError

  • java.lang.OutOfMemoryError: Java heap space

This error will be triggered when the application attempts to add more data into the heap space area, but there is not enough room for it. Note that there might be plenty of physical memory available, but the java.lang.OutOfMemoryError: Java heap space error is thrown whenever the JVM reaches the heap size limit.

  • java.lang.OutOfMemoryError: GC overhead limit exceeded

This error means that the GC tried to free memory but was pretty much unable to get anything done. By default, it happens when the JVM spends more than 98% of the total time in GC and when after GC less than 2% of the heap is recovered.

  • java.lang.OutOfMemoryError: Metaspace/Compressed class space

The main cause for this error is either too many classes or too big classes being loaded to the Metaspace.

  • java.lang.OutOfMemoryError: Unable to create new native thread

Whenever the underlying OS cannot allocate a new native thread, this OutOfMemoryError will be thrown. The exact limit for native threads is very platform-dependent

  • java.lang.OutOfMemoryError: Out of swap space?

This error is thrown by JVM when an allocation request for bytes from the native heap fails and the native heap is close to exhaustion. It is often caused by operating system level issues, such as:

  • The operating system is configured with insufficient swap space.

  • Another process on the system is consuming all memory resources.

    • java.lang.OutOfMemoryError: Requested array size exceeds VM limit

This means that the application that crashes with the error is trying to allocate an array larger than the Java Virtual Machine can support