Heap Dump – Overview

Heap Dump Introduction
Figure 01

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 a 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 analyse 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 heap memory leakage issue. 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?

A 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 Classes, 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:

  1. Auto-generated: Using the below command-line argument you can instruct JVM to generate the heap dump when out of memory event occurs.
  2. Manually: To generate a heap dump manually use the below argument:
    jmap -dump:format=b,file=<filename in .hprof/.bin format> <processID>

Important Terminologies:

  1. Shallow heap: The amount of memory that is occupied by one object.
  2. 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.
  3. 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 objects) of each object is 1 byte. The retained heap of Object O4, O5, O6 and O7 will be 1 byte because they do not hold a reference to any object. Now the calculation of retained heap of other objects is:

  1. 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).
  2. 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).
  3. 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 a retained set of O1.

Shallow and Retained Heap
Figure 02: Shallow and Retained Heap

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 a 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 combined 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 to the following posts to learn heap dump analysis. The post writer did really good work and wrote the content in an easy language.

  1. Memory Analyzer Blog
  2. EclipseSource

In Addition, you can get MAT (Eclipse – Memory Analysis Tool) learning document here.

Heap Dump Analysis Tool:

You may be interested: