Understanding Java Heap Dumps: Shallow Heap, Retained Heap, Dominator Tree, and GC Roots
This article explains Java heap dump files, detailing the concepts of shallow and retained heap, leading sets, dominator trees, and various types of garbage collection roots to help developers analyze memory usage and diagnose OutOfMemoryError issues.
Heap Dump (Java heap dump file) represents a snapshot of the JVM memory at a specific point in time, typically captured when an OutOfMemoryError occurs. Depending on the dump format and JVM type, the file contains information about classes and objects present in the heap, usually after a Full GC, so only objects that cannot be reclaimed remain.
In most dumps, the following are included:
All objects, including classes, fields, primitive data (int, double, etc.), and references.
All classes, along with their class loaders, names, superclasses, and static fields.
Garbage collection root objects – objects reachable by the JVM.
Thread stacks and local variables – stack frames and local variable information for all threads at the time of the snapshot.
The dump does not contain allocation address information, so it cannot reveal which object created another or when an object was instantiated.
Shallow Heap and Retained Heap
Shallow heap is the memory occupied by the object itself (e.g., a reference occupies 32 or 64 bits, an integer occupies 4 bytes).
Retained set is the collection of objects that would be removed by the GC when the object is reclaimed; if object Y can only be accessed through X, Y belongs to X's retained set.
Retained heap is the sum of the shallow heap sizes of all objects in the retained set, representing the total memory retained because of the object.
The leading set is a retained set of a group of objects (e.g., all instances of a class or all objects loaded by a specific class loader); its retained heap is the sum of shallow heaps of all objects in that set.
In summary, an object's shallow heap is its own size, while its retained heap is the total memory that would be freed when the object is collected; the leading set aggregates the retained sets of its members.
In the diagram, A and B are GC root objects such as method parameters, native objects created locally, or objects used for wait(), notify(), and synchronized() operations.
leading set
Retained set
G
G
E1,E2
E1,E2,G,I
C
C,D,E1,E2,F,G,H,I
A,B
A,B,C,D,E1,E2,F,G,H,I
Dominator Tree
MAT provides a Dominator Tree derived from the object graph in a heap dump, helping quickly locate the largest memory consumers and analyze object dependencies. Key definitions:
An object X dominates object Y if every path to Y in the object graph passes through X.
The immediate dominator of Y is the closest dominator to Y in the graph.
The Dominator Tree is built from the object graph; each node’s children are the objects it directly dominates.
Properties of the Dominator Tree:
The sub‑tree of X (objects dominated by X) corresponds to X’s retained set.
If X directly dominates Y, then X’s dominator also dominates Y, and so on.
The Dominator Tree does not directly reflect reference relationships in the original object graph.
Garbage Collection Roots (GC Roots)
A GC root is an object reachable from outside the heap. Objects become GC roots for various reasons, including:
System classes loaded by bootstrap or system class loaders (e.g., java.util.* in rt.jar).
JNI local variables in native code.
JNI global variables in native code.
Thread blocks referencing objects.
Active threads.
Objects holding monitors (used in wait(), notify(), or synchronized).
Java local variables such as method parameters or objects created inside a method and still on the thread stack.
Native stack parameters (common for methods with native parts).
Finalizable objects awaiting finalizer execution.
Unfinalized objects with a finalize method but not yet queued.
Unreachable objects marked as roots by MAT to retain them for analysis.
Java stack frames holding local variables (when parsed as objects).
Unknown categories.
© Content sourced from the internet; original author retains copyright. Original link: http://fengyilin.iteye.com/blog/2430305
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.