Troubleshooting OutOfMemoryError or any memory related problem is done manually even in 2019. Troubleshooting and identifying the root cause of OutOfMemoryError can even be automated, by following below mentioned 3 steps:
- Capture heap dump
- Restart application
- Problem Diagnosis
Let’s discuss these steps in detail.
1. Capture heap dump
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}
Example:
-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 a 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 an erroneous result.
You can automate this restart process as well. Write a “restart-myapp.sh” script, which will shutdown and restart your application gracefully. Now specify this script’s path in ‘-XX:OnOutOfMemoryError’ JVM argument.
Example:
-XX:OnOutOfMemoryError=/scripts/restart-myapp.sh
When you pass this argument, JVM will invoke “/scripts/restart-myapp.sh” 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, 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 ‘restart-myapp.sh’ script. Thus, you will be able to automate OutOfMemoryError troubleshooting end-end.
Reference Article:https://blog.gceasy.io/2019/06/12/au...oubleshooting/