Refer to the above figure (Figure 01). You can see two snaps of the same room. Would you be able to find the difference between them? I assume your answer will be ‘Yes’. Because you can easily visualize them and identify the differences using your senses. Finding the difference in the objects and their condition is bit easy and comparable with a snap. But this is something which we can see in real life. What about the computer (JVM) world? How will you analysis the status of the objects or any memory leak problem? There must be some kind of snap or data which can help you to solve the purpose of heap memory leak. So, the answer is ‘Heap Dump’.
Heap Dump provides a snap of objects and their status in the memory at a particular time. Such snaps are comparable and help to identify the root cause related to a memory leak. So let’s start the discussion with some basics:
What is Memory Heap?
Heap is a portion of memory which is used for dynamic allocation. The blocks of the memory in the heap are frequently allocated and freed based on the status of the object. The allocation and release of memory take place in an arbitrary order.
Get more information on Memory Heap and Generations …
What is Heap Dump?
JVM heap dump is an informative data (sometimes it is called a snap) of Java heap memory which contain the low-level details about java objects and classes allocated in the Java heap at the moment when the data is fetched or snapshot is triggered. Generally, a heap dump is triggered after the full GC ran so that the dump contains the information about the remaining objects in the heap. A heap dump contains the information about:
- Java objects such as Class, fields, primitive values and references
- Classloader related data, name, superclass, and static fields (important for classloader leak problems)
- Garbage collection roots or objects that are accessible from outside the heap (System classloader loaded resources such as rt.jar, JNI or native variables, Threads, Java Locals and more…)
- Thread related data & stacks (very useful for sudden Java heap increase problems, especially when combined with thread dump analysis)
A heap dump does not contain memory allocation information, therefore you cannot work out what created the objects or where the objects were created.
How to generate a Heap Dump?
There are 2 methods to generate the heap dump:
- Auto-generated: Using below command-line argument you can instruct JVM to generate the heap dump when out of memory event occurs.
- Manually: To generate heap dump manually use the below argument:
jmap -dump:format=b,file=<filename in .hprof/.bin format> <processID>
- Shallow heap: The amount of memory that is occupied by one object.
- Retained set: The set of objects that are referenced, directly or indirectly from the original object. When an original object is garbage collected then retained set of objects will also be removed.
- Retained heap: The Retained heap is the sum of the shallow sizes of all objects in the retained set which is freed when the original object is garbage collected.
Refer to the below figure (Figure 02). Consider there is a heap memory in which 7 objects are present. The shallow heap (memory of individual object) of each object is 1byte. The retained heap of Object O4, O5, O6 and O7 will be 1byte because they do not hold a reference to any object. Now the calculation of retained heap of other objects is:
- Retained Heap of O2: Object O2 holds the reference to O5 and O6. Hence the retained heap of O2 will be the sum of the shallow heap of self (O2), O5 and O6 which is 3bytes. (=1 + 1 + 1).
- Retained Heap of O3: Object O3 holds the reference to O7. Hence the retained heap of O3 will be the sum of the shallow heap of self (O3) and O7 which is 2. (=1 + 1).
- Retained Heap of O1: Object O1 holds the reference to O2, O3 and O4. In addition, O2 holds the reference to O5 and O6 and O3 holds the reference to O6. Hence the retained heap of O1 will be the sum of the shallow heap of self (O1), O2, O3, O4, O5, O6 and O7 which is 7. (= 1 + 1 + 1 + 1 + 1 + 1 + 1).
Similarly, in the retained set of O2 the object O5 and O6 exist, O7 lies in the retained set O3 and all the objects i.e. O2, O3, O4, O5, O6 and O7 come under retained set of O1.
What is Heap Dump Analysis?
Just compare with the example which I have shown you above. Once you get the heap dump (or snap), you can easily analyse the status of the objects and classes available in the heap memory at the particular time. The main purpose of heap dump analysis is to identify any heap memory leak, Java classloader memory leaks and sudden Java heap increase problems or trigger events (when combining with thread dump analysis). Also, it helps in finding a memory leak involves looking for the largest objects, examining the content of those objects, and finding which objects are keeping those large objects in the heap.
You can refer the following posts to learn heap dump analysis. The post writer did really good work and written the content in an easy language.
In Addition, you can get MAT (Eclipse – Memory Analysis Tool) learning document here.
Heap Dump Analysis Tool:
You may be interested: