Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

In a heap dump analyser such as Eclipse MAT, explain shallow size, retained size and the dominator tree, and how you use them to find what is holding memory.

Shallow size is the memory taken by an object itself, its header and fields; retained size is the memory that would be freed if that object were collected, meaning the object plus everything only it keeps alive. Object X dominates object Y if every path from the GC roots to Y passes through X, and the dominator tree arranges the heap by these owners. To find what holds memory, we sort the dominator tree by retained size, take the few objects at the top, ask for their path to GC roots excluding weak and soft references to see why they are still reachable, and compare class histograms from two dumps taken some time apart to see which types grow.

Why this question is asked

Opening a heap dump for the first time is overwhelming: millions of objects, and the largest classes are almost always byte arrays and strings, which tells us nothing. The concepts of retained size and dominators are what turn that pile into an answer like this one cache holds 1.4 GB. Interviewers ask about them because they separate people who can actually analyse a dump from people who only know how to take one. In this answer, we will define shallow and retained size with a small example, explain dominators and the dominator tree, follow the workflow from the dominator tree to the path to GC roots, and finish with comparing histograms over time.

1Shallow Size: The Object Itself

The shallow size of an object is the memory the object occupies on its own: the object header plus its fields. For a reference field, only the reference counts, typically 4 bytes with compressed references, not the object it points to. So a HashMap object has a small shallow size, around 48 bytes, whether it holds two entries or two million, because its entries live in a separate array and separate node objects.

Shallow size is why class histograms are hard to read on their own. They show that byte[] and String use the most memory, which is true for almost every Java application, but they do not say who owns those arrays.

2Retained Size: What Would Be Freed

The retained size of an object is the total memory that would be freed if that object were removed and the collector ran: the object's own shallow size plus the shallow size of every object that is reachable only through it. Objects that are also reachable some other way are not counted, because removing our object would not free them.

Java
// a cache that grows without a bound
class SessionCache {
private final Map<String, Session> sessions = new HashMap<>(); // shallow: SessionCache about 16 bytes
}
// in MAT, for the single SessionCache instance:
// Shallow Heap 16 B
// Retained Heap 1.41 GB <- the map, its table, 2.1 million nodes, Sessions and their data
// a String shared with another live object is not in the retained size:
// freeing the cache would not free it

Notice the contrast: a 16-byte object retains 1.41 GB. That is the number that matters when we hunt for a leak, because it tells us how much memory we would get back by fixing that one owner.

3Dominators and the Dominator Tree

MAT computes retained sizes with the idea of domination. Object X dominates object Y if every path from any GC root to Y goes through X. GC roots are the starting points of reachability, such as local variables on thread stacks, static fields of loaded classes and JNI references. If X dominates Y, then removing X makes Y unreachable, so Y is part of X's retained set. The closest such X is Y's immediate dominator.

If we draw each object under its immediate dominator, we get the dominator tree. Its top level holds objects that nothing else dominates, and each node's retained size is the sum of its subtree. It is a regrouping of the heap by ownership.

A small object graph and its dominators
ObjectReachable fromImmediate dominatorRetained?
SessionCachestatic field (GC root)GC rooteverything below
HashMapSessionCache onlySessionCacheyes, in cache's set
Node[] table and nodesHashMap onlyHashMapyes
Session objectsnodes onlytheir Nodeyes
Shared Localea Session and a static fieldGC rootno, reachable elsewhere
An object reachable by two independent paths is dominated only by the root, so it never counts in either path's retained size.

4Finding What Holds Memory

The workflow follows from those definitions. We open the dump in MAT and go to the dominator tree, sorted by retained heap. In a leaking application the top few entries usually account for most of the heap, for example one SessionCache retaining 1.41 GB of a 2 GB heap. Expanding that entry shows the structure inside: the HashMap, its table and the millions of entries. MAT's Leak Suspects report does a version of this automatically and is a good first click.

Knowing the big owner is half the answer; we also need to know why it is still alive. For that we use Path to GC Roots on the suspect, with the option to exclude weak and soft references. Weak and soft references do not keep an object alive in a leak sense, since the collector may clear them, so including them only clutters the result. The remaining path shows the chain of strong references, for example from a static field in a class, through a Spring singleton bean, to the map.

Step throughFrom dump to cause
  1. Open the dump, run Leak Suspectsquick overview
  2. Dominator tree, sort by retained heaptop entries hold most of the heap
  3. Expand the suspecte.g. SessionCache -> HashMap -> 2.1M entries
  4. Path to GC roots, exclude weak and softwho keeps it alive?
  5. Is the growth expected?
    YesSize the heap or bound the dataNoFix the leak: eviction, removal, scope

5 steps in order. Play them, or step through them one at a time.

5Comparing Histograms Over Time

A single dump shows what is big, but not what is growing. A large cache may be perfectly healthy if it has a fixed size. To find growth, we compare two snapshots taken some time apart under similar load, for example an hour apart. MAT can compare the class histograms of two dumps, and for a quicker check we can compare histograms from jcmd without taking full dumps.

Shell
jcmd 4711 GC.class_histogram > histo-10h00.txt
# ... one hour later
jcmd 4711 GC.class_histogram > histo-11h00.txt
# 10:00 11:00
# com.acme.Session 812,400 -> 1,214,950 instances (+50 percent)
# java.util.HashMap$Node 2,103,991 -> 3,050,112
# java.lang.String 3,911,034 -> 3,955,870 (stable)

The types whose counts keep climbing between snapshots, here Session and HashMap$Node, point at the leak, and the dominator tree of a dump then shows who owns them. Notice that String barely changed, although it is near the top of the histogram by size, which is why growth is a better signal than size.

6Follow-up questions

Why are retained sizes not available instantly when MAT opens a dump?

Computing dominators requires an analysis of the whole object graph. MAT does this when it parses the dump and stores index files next to it, which is why the first opening of a large dump takes a long time and needs a lot of memory, but later openings are fast.

Can the sum of retained sizes of several objects exceed the heap?

Yes, if we add up objects where one dominates another, because the inner object's retained set is included in the outer one's. Retained sizes of siblings at the same level of the dominator tree, such as the top-level entries, do not overlap.

What if the dominator tree shows no single big owner?

Then memory is spread across many small owners, for example many ThreadLocals or many request objects. Grouping the dominator tree by class or by class loader, or looking at the histogram of retained sizes per class, helps find the common type.

7Conclusion

In this answer, we saw that shallow size is the memory of the object itself and retained size is what would be freed if it were collected. An object dominates another when every path from the GC roots passes through it, and the dominator tree groups the heap by these owners. We sorted the dominator tree by retained size to find the few owners holding most of the heap, used path to GC roots without weak and soft references to see why they live, and compared histograms over time to find which types grow.

What to say in the interviewAbout 90 seconds aloud

Open with

Shallow size is just the object itself, its header and fields, with reference fields counted as references only. So a HashMap object is tiny whether it holds two entries or two million.

Then, if they want more

Retained size is what would be freed if that object were collected: the object plus everything reachable only through it. MAT computes that through domination. X dominates Y if every path from the GC roots to Y goes through X, so removing X makes Y unreachable.

Arranging every object under its closest dominator gives the dominator tree, and each node's retained size is its whole subtree. Anything reachable by an independent path, like a shared string, isn't counted. To find what's holding memory, I open the dominator tree sorted by retained heap.

In a leak, the top few entries usually hold most of the heap, say one SessionCache retaining 1.4 gigabytes.

I expand it to see the structure, and then run path to GC roots, excluding weak and soft references because those don't really keep things alive, to see the chain of strong references, maybe a static field through a singleton bean to the map.

And because big isn't the same as growing, I compare class histograms from two dumps or two jcmd histograms an hour apart; the types whose counts keep climbing are the leak.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "In a heap dump analyser such as Eclipse MAT, explain shallow size, retained size and the...": https://scrivio-interview-library.netlify.app/java/jvm-diagnostics/8069c22698/