Figure 7-1. Object graph of retained memory
SHALLOW, RETAINED, AND DEEP OBJECT SIZES
Two other useful terms for memory analysis are shallow and deep. The shallow size of an object
is the size of the object itself. If the object contains a reference to another object, the 4 or 8 bytes
of the reference is included, but the size of the target object is not included.
The deep size of an object includes the size of those objects. The difference between the deep size
of an object and the retained memory of an object lies in objects that are otherwise shared. In Fig-
whereas the retained size of the Flute Duo object does not.
Objects that retain a large amount of heap space are often called the dominators of the heap.
If the heap analysis tool shows that there are a few objects that dominate the bulk of the
heap, then things are easy: all you need to do is create fewer of them, retain them for a short-
er period of time, simplify their object graph, or make them smaller. That may be easier said
than done, but at least the analysis is simple.
More commonly, some detective work will be necessary because the program is likely shar-
ing objects. Like the Michael object in the previous figure, those shared objects are not coun-
ted in the retained set of any other object, since freeing one individual object will not free the
shared object. Also, the largest retained sizes are often classloaders over which you have
little control. As an extreme example, Figure 7-2 shows the top retained objects of a heap
from a version of the stock servlet running in GlassFish that caches items strongly in the