Thursday, August 1, 2013

Heap Memory Analysis # Shallow heap vs Retained Heap

Shallow heap
The amount of memory that is consumed by one object. An object requires different amounts of memory depending on the operating system architecture. For example, 32 bits or 64 bits for a reference, 4 bytes for an integer, or 8 bytes for an object of type "Long". Depending on the heap dump format, the size might be adjusted to provide a more realistic consumption of the JVM.

Retained Set
One or more objects plus any objects that are referenced, directly or indirectly, only from those original objects. The retained set is the set of objects that would be removed by garbage collection when an object, or multiple objects, is garbage collected.

Retained set of X is the set of objects which would be removed by GC when X is garbage collected.

Retained heap of X is the sum of shallow sizes of all objects in the retained set of X, i.e. memory kept alive by X.

Retained heap, or retained size
The total heap size of all the objects in the retained set. This value is the amount of memory that is consumed by all the objects that are kept alive by the objects at the root of the retained set

Generally speaking, shallow heap of an object is its size in the heap and retained size of the same object is the amount of heap memory that will be freed when the object is garbage collected.

The retained set for a leading set of objects, such as all objects of a particular class or all objects of all classes loaded by a particular class loader or simply a bunch of arbitrary objects, is the set of objects that is released if all objects of that leading set become unaccessible. The retained set includes these objects as well as all other objects only accessible through these objects. The retained size is the total heap size of all objects contained in the retained set.


Example object graph



Example, if you remove C, then C, D, E, F, G, H are eligible for garbage collection.

No comments:

Post a Comment