The differences between shallow and retained heap

Discussions

News: The differences between shallow and retained heap

  1. The shallow heap is easy – it consists of only the heap occupied by the object itself. There are some nuances to how to calculate it, but for the scope of this article we leave it as is. 

    The retained heap is in many ways more interesting. Only rarely are you interested in the shallow heap, in most cases your actual question can be translated to “If I remove this object from the memory, how much memory can now be freed by the garbage collector”.

    I have created an and explained how to calculate the retained heap in the blog post here.

  2. Shallow and retained heap An explanation of terms that relate to the Java heap. The following terms are used to describe objects in the Java 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. The following diagram represents objects in the Java heap. Objects A and B are garbage collection roots, for example method parameters, locally created objects, or objects that are used for wait(), notify(), or synchronized() methods. The set of objects A and B has a retained set consisting of objects A, B, C, D, E, F, G, and H.