the two nouns of the shallow heap and retained heap are often encountered in Java memory analysis. This post is a translation of the shallow heap and retained heap help documentation in Eclipse Memory Analyzer.
The shallow heap is the memory consumed by an object. In an object, the reference property requires 32 or 64bits (depending on the OS architecture), the integer attribute requires 4 bytes, the Long property requires 8 bytes, and so on. Depending on the format of the heap dump, this size may be adjusted (for example, aligned to 8 bytes, etc.) to a model that is more conducive to the actual memory consumption of the VM.
The retained set of X is the collection of all objects that will be recycled by GC when x is garbage collected.
The retained heap of x refers to the shallow size of all objects in the retained set of X and, for example, memory that is surviving because of X.
Generally, the shallow heap of an object is the size of the object in the heap, and the retained size of the same object is the sum of the heap memory that will be freed when the object is garbage collected.
From the leading set angle, retained set is a collection of objects that are freed because leading set becomes unreachable. The leading set can be any object of a particular class, or an object of all classes loaded by a particular class loader, or a combination of arbitrary objects. The retained set includes objects in the leading set and all objects that can only be accessed through objects in leading set. Retained size is the total heap size of all objects in the retained set.
Shallow vs. retained Heap