Shallow Size Vs Retained Size

  1. Shallow Size Vs Retained Size Calculator
  2. Shallow Size Retained Size Java

Lets start calculating the retained sizes of those objects. O4 has no references to other objects, so its retained size is equal to its shallow size of 1kB. O3 has a reference to O4. Garbage collecting O3 would thus mean O4 would also be eligible for garbage collection and so we can say that O3 retained heap is 2kB. O2 has a reference to O3. Shallow size of a set of objects represents the sum of shallow sizes of all objects i Java堆:Shallow Size和Retained Size kingzone2008 2013-06-13 11:9 收藏 5. Shallow Size: Total amount of Java memory used by this object type (in bytes). Retained Size: Total size of memory being retained due to all instances of this class (in bytes). You can use the two menus above the list of allocated objects to choose which heap dumps to inspect and how to organize the data. May 04, 2021 Shallow size: Displays the sum of shallow sizes of all objects created by a certain constructor function. The shallow size is the size of memory held by an object (generally, arrays and strings have larger shallow sizes). Navigate to Object sizes. Retained size: Displays the maximum retained size among the same set of objects. Shallow size: the size of the object itself; retained size: the size of the object itself, plus the size of other objects that are kept alive by this object; Often, objects will have a small shallow size but a much larger retained size, through the references they contain to other objects.

Shallow size vs retained size measurement

Eclipse MAT (Memory Analyzer Tool) is a powerful tool to analyze heap dumps. It comes quite handy when you are trying to debug memory related problems. In Eclipse MAT, two types of object sizes are reported:

  1. Shallow Heap
  2. Retained Heap

In this article, let's study the difference between them and explore how they are calculated

Fig 1: Objects in memory

It’s easier to learn new concepts through example. Let’s say your application has an object model, as shown in Fig #1:

  • Object A is holding a reference to objects B and C.
  • Object B is holding a reference to objects D and E.
  • Object C is holding a reference to objects F and G.

Let’s say each object occupies 10 bytes of memory. Now, with this context, let’s begin our study.

Shallow Heap Size

Remember: the shallow heap of an object is its size in the memory. Since, in our example, each object occupies about 10 bytes, the shallow heap size of each object is 10 bytes. Very simple.

Retained Heap Size of B

From Fig #1, you can notice that object B is holding a reference to objects D and E. So if object B is garbage collected from memory, there will be no more active references to object D and E. It means D and E can also be garbage collected. Retained heap is the amount of memory that will be freed when the particular object is garbage collected. Thus, the retained heap size of B is:

= B’s shallow heap size + D’s shallow heap size + E’s shallow heap size

= 10 bytes + 10 bytes + 10 bytes

= 30 bytes

Thus, the retained heap size of B is 30 bytes.

Retained Heap Size of C

Object C is holding a reference to objects F and G. So, if object C is garbage collected from memory, there will be no more references to object F and G. It means F and G can also be garbage collected. Thuthe s, retained heap size of C is:

Size

= C’s shallow heap size + F’s shallow heap size + G’s shallow heap size

= 10 bytes + 10 bytes + 10 bytes

= 30 bytes

Thus the, retained heap size of C is 30 bytes as well.

Fig 2: Objects Shallow and Retained Heap size

Retained Heap Size of A

Object A is holding a reference to objects B and C, which, in turn, are holding references to objects D, E, F, and G. Thus, if object A is garbage collected from memory, there will be no more reference to object B, C, D, E, F, and G. With this understanding, let’s complete a retained heap size calculation of A.

Thus,the retained heap size of A is:

= A’s shallow heap size + B’s shallow heap size + C’s shallow heap size + D’s shallow heap size + E’s shallow heap size + F’s shallow heap size + G’s shallow heap size

= 10 bytes + 10 bytes + 10 bytes + 10 bytes + 10 bytes + 10 bytes + 10 bytes

Shallow Size Vs Retained Size

= 70 bytes

We can then conclude that the retained heap size of A is 70 bytes.

Retained heap Size of D, E, F, and G

Retained heap size of D is 10 bytes, however, this only includes their shallow size. This is because D doesn't hold any active reference to any other objects. Thus, if D gets garbage collected, no other objects will be removed from memory. As per the same explanation objects, E, F, and G’s retained heap sizes are also only 10 bytes.

Let’s Make Our Study More Interesting

Now, let’s make our study a little bit more interesting, so that you will gain a thorough understanding of shallow heap and retained heap size. Let’s have object H starts to hold a reference to B in the example. Note object B is already referenced by object A. Now, two guys A and H are holding references to object B. In this circumstance, lets study what will happen to our retained heap calculation.

Fig 3: New reference to Object B

Shallow Size Vs Retained Size Calculator

In this circumstance, retained heap size of object A will go down to 40 bytes. Surprising? Puzzling?

If object A gets garbage collected, then there will be no more reference to objects C, F, and G only. Thus, only objects C, F, and G will be garbage collected. On the other hand, objects B, D, and E will continue to live in memory because H is holding an active reference to B. Thus, B, D, and E will not be removed from memory even when A gets garbage collected.

Thus, the retained heap size of A is:

= A’s shallow heap size + C’s shallow heap size + F’s shallow heap size + G’s shallow heap size

= 10 bytes + 10 bytes + 10 bytes + 10 bytes

= 40 bytes.

The total retained heap size of A will become 40 bytes. All other objects retained heap size will remain undisturbed, because there is no change in their references.

Shallow Size Retained Size Java

Hope this article helped to clarify Shallow heap size and Retained heap size calculation in Eclipse MAT. You might also consider exploring HeapHero – another powerful heap dump analysis tool, which shows the amount of memory wasted due to inefficient programming practices such as duplication of objects, overallocation and underutilization of data structures, suboptimal data type definitions,….