Figure 01
See Figure 01 above for further information. Two pictures of the same room are displayed. Would you be able to distinguish between the two of them? I’m going to assume that you’ll say “Yes.” Because using your senses, you can easily distinguish between them and see them. Finding the differences among the objects and comparing them quickly is quite simple. But we can actually observe this in the real world. What about the world of computers (JVM)? How will you evaluate the objects’ state and any memory leak issues? You must be able to tackle the problem of a heap memory leak using some sort of snap or data. So, “Heap Dump” is the answer.
Heap Dump offers a snapshot of the items and their location in the memory at a specific moment. These snaps are equivalent and help in locating the source of a memory leak. So let’s begin the conversation with some fundamentals:
Describes Memory Heap.
Memory that is used for dynamic allocation is called a heap. According to the object’s status, memory blocks in the heap are often allocated and released. Memory is allocated and released in an arbitrary sequence.
Heap Dump: What is it?
JVM heap dumps are useful data files (also known as snapshots) that include low-level information about Java objects and classes that are currently being allocated in the Java heap at the time the data is fetched or the snapshot is triggered. In order for the dump to contain information about the remaining items on the heap, a heap dump is often started after the whole GC has been completed. A heap dump includes the following data:
- Classes, fields, primitive values, and references are examples of Java objects.
- Name, superclass, and static fields pertaining to classloaders are crucial for classloader leak issues.
- Objects or garbage collection roots that are reachable from outside the heap (System classloader-loaded resources like rt. Threads, Java Locals, JNI versus native variables, and more…)
- Data and stacks related to threads (particularly helpful in conjunction with thread dump analysis for problems involving rapid Java memory increases)
You cannot determine what created the objects or where they were created from a heap dump since it does not contain memory allocation information.
How is a heap dump generated?
The heap dump can be created using any of two methods:
- Auto-generated: You can tell the JVM to create the heap dump when an out-of-memory event takes place by using the command-line parameter listed below.
-XX:+HeapDumpOnOutOfMemoryError - Manually: Use the following parameter to manually generate a heap dump:
Filename in.hprof/.bin format: jmap -dump:format=b,file:processID>
Essential Terms
- Shallow heap: One object’s share of memory is referred to as a shallow heap.
- Retained set: The group of items to which the original object, either directly or indirectly, refers is known as the retained set. Retained collection of objects will likely be destroyed when an original object is garbage collected.
- Retained heap: When an original object is garbage collected, the retained heap—which is made up of all the shallow sizes of the objects in the retained set—is released.
Example:
Refer to Figure 02 in the example below. Imagine that there is a heap memory with 7 objects. Each object has a 1byte shallow heap (memory of individual objects). Due to the fact that they do not contain a reference to any objects, the retained heap of objects O4, O5, O6, and O7 will be 1 byte. The remaining pile of other things has now been calculated as follows:
Retained Heap of O2: The reference to O5 and O6 is stored in object O2. As a result, the shallow heap of self (O2), O5 and O6 will be added to the retained heap of O2, which is 3 bytes. (=1 + 1 + 1).
Object O3’s Retained Heap: O3 is where the reference to O7 is stored. As a result, the shallow heap of self (O3) and O7, which is 2, will be added to the retained heap of O3 (=1 + 1).
Retained Heap of O1: The reference to O2, O3, and O4 is stored in object O1. In addition, O2 and O3 both hold a reference to O5, and O6 is held by O2. As a result, the shallow heap of self (O1), O2, O3, O4, O5, O6, and O7 will be added to the retained heap of O1 to yield the value 7 (= 1 + 1 + 1 + 1 + 1 + 1).
Similar to this, the objects O5 and O6 are in the retained set of O2, O7 is in the retained set of O3, and all of the objects, including O2, O3, O4, O5, O6, and O7, are in the retained set of O1.
Figure 02: Shallow and Retained Heap
Heap Dump Analysis: What Is It?
Just contrast it with the earlier example I gave you. You may quickly analyze the state of the objects and classes present in the heap memory at a given time once you have the heap dump (or snap). The main goal of a heap dump analysis is to locate any memory leaks in the heap, memory leaks in Java classloaders, problems with a sudden growth in the Java heap, and trigger events (when combined with a thread dump analysis). Finding a memory leak also entails looking for the largest things in the heap, going through their contents, and figuring out which other objects are keeping those enormous objects there.
To learn heap dump analysis, you can refer to the following posts. The post’s author did an excellent job and used simple language to write the content.
- A blog for Memory Analyzer
- EclipseSource
Tool for Heap Dump Analysis:
- MAT
- VisualVM
- IBM HeapAnalyzer
- HeapHero (Online Heap Analyzer)