Troubleshooting OutOfMemoryError or any memory related problem is done in manually even in 2019. Troubleshooting and identifying the root cause of OutOfMemoryError can even be automated, by following below mentioned 3 steps:        

  1. Capture heap dump
  2. Restart application
  3. Problem Diagnosis

Let’s discuss these steps in detail.

1. Capture heap dump

Heap dump is basically 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 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 specified file path, when OutOfMemoryError is thrown.

2. Restart application

Most of the time, OutOfMemoryError doesn’t crash the application. However, it’s better to restart the application, once OutOfMemoryError is thrown. Because OutOfMemoryError can potentially leave application in an unstable state. Requests served from an unstable application instance can lead to erroneous result.

You can automate this restart process as well. Write a “” script, which will shutdown and restart your application gracefully. Now specify this script’s path in ‘-XX:OnOutOfMemoryError’ JVM argument.



When you pass this argument, JVM will invoke “/scripts/” script whenever OutOfMemoryError is thrown. Thus, your application will be automatically restarted right after it experiences OutOfMemoryError.

3. Problem Diagnosis

Now we have captured the heap dump (which is needed to troubleshoot the problem), restarted the application (to reduce the outage impact). Next step is troubleshooting. This can be little tricky 😊, but can be achieved with the right tools. You can use tools like Eclipse MAT, HeapHero to analyze heap dumps. These tools generate good memory analysis report, highlighting the objects that are causing memory leak. However, the most organization does this step is manually.

Even this step can be automated by invoking HeapHero REST API. This API analyzes heap dump and returns excellent analysis report. You may invoke this API right after ‘’ script. Thus, you will be able to automate OutOfMemoryError troubleshooting end-end.

Happy Troubleshooting.

Leave a Reply

Powered by

Up ↑

%d bloggers like this: