• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to automate – OutOfMemoryError Troubleshooting

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:

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 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:



Example:



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:



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.

Happy Troubleshooting.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!