JVM arguments to deal with OutOfMemoryError

What are the JVM arguments to deal with OutOfMemoryError


Following documentation describes heap size settings:

and is a good place to start in case your system is not configured with enough heap to handle your load.

Also, it is important to know whether you are running out of heap memory or native memory, the exception message should be able to tell that.

  1. -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath
  2. -XX:OnOutOfMemoryError
  3. -XX:+ExitOnOutOfMemoryError
  4. -XX:+CrashOnOutOfMemoryError

(1). -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath

Heap dump is basically a snapshot of memory. It contains details about objects that present in memory, actual data that is present within those objects, references originating of those objects. Heap dump is a vital artifact to troubleshoot memory problems.

In order to diagnose OutOfMemoryError or any memory related problem, one would have to capture heap dump right at the moment or few moments before the application starts to experience OutOfMemoryError. It’s hard to do capture heap dump at the right moment manually because we will not know when OutOfMemoryError is going to be thrown. However, capturing heap dumps can be automated by passing following JVM arguments when you launch the application in the command line:

-XX:+HeapDumpOnOutOfMemoryError and -XX:HeapDumpPath={HEAP-DUMP-FILE-PATH}


-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/crashes/my-heap-dump.hprof

In ‘-XX:HeapDumpPath’ you need to specify the filepath where heap dump should be stored.

When you pass these two JVM arguments, heap dumps will be automatically captured and written to the specified file path, when OutOfMemoryError is thrown.

Once heap dumps are captured, you can use tools like HeapHero, Eclipse MAT to analyze heap dumps.

More details can be found here..

Rather than trying to tune the JVM it may be useful to see what is using the heap. What is the specification of you cluster? What is your use case?