How to diagnose memory leaks in java

Memory leaks doesn't have to be hard/scary/tedious problem to solve, if you follow below mentioned 3 simple steps:

Step 1: Capture baseline heap dump

You need to capture heap dump when it’s in healthy state. Start your application. Let it take real traffic for 10 minutes. At this point capture heap dump. Heap Dump is basically snapshot of your memory. It contains all objects that are residing in the memory, values stored in those objects, inbound & outbound references of those object. You can capture Heap dump can be captured using following command:


jmap -dump:format=b,file=<file-path> <pid>

where

pid: is the Java Process Id, whose heap dump should be captured

file-path: is the file path where heap dump will be written in to.

If you don't want to use jmap for capturing heap dumps, here are several other options to capture heap dumps


It’s always better to capture heap dump in the production environment (unless in the test environment you can mirror the exact production traffic pattern). Traffic type and its volume plays a primary role in the type and number of objects created in the memory.


Step 2: Capture troubled heap dump


After doing step #1, let the application run. Before application crashes take another heap dump once again. Often times it might be challenging to capture heap dumps before it crashes, because we don't know when application will crash. Is it after 30 minutes, 3 hours, 3 days? Thus, it's ideal to start your application with following JVM property:



-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=<file-path>


file-path: is the file path where heap dump will be written in to.



This property will trigger heap dump right when application experiences OutOfMemoryError.


Step 3: Compare heap dumps


Objects which are causing memory leaks grow over the period. If you can the identify objects whose size has grown between the heap dumps captured in step #1 and step #2, then those are the objects which is causing memory leak.



You can consider using heap dump analyzer tool such as HeapHero.io for this purpose. When you load the heap dumps in to heapHero.io, it provides rich information about your application's memory. There is a "Large Objects" section, which reports largest objects that are sitting in the memory. Compare this section between heap dumps captured in step #1 and step #2. If you notice any abnormal growth of objects, then they are the ones which is causing memory leak in your application. You can also click on any of the largest object to see the children, grandchildren, great grandchildren objects present in it.

Author bio:

Every single day, millions & millions of people in North America—bank, travel, and commerce—use the applications that Ram Lakshmanan has architected. Ram is an acclaimed speaker in major conferences on scalability, availability, and performance topics. Recently, he has founded a startup, which specializes in troubleshooting performance problems.










2 Responses to "How to diagnose memory leaks in java"
  1. weqr 2018-05-21 16:25:45.0
  1. TVD 2018-08-18 03:46:56.0

Your email address will not be published. Required fields are marked *