- Chrome Inspect Shallow Size Vs Retained Size
- Shallow Size Vs Retained Size Calculator
- Shallow Size Vs Retained Size Formula
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. A sieve analysis (or gradation test) is a practice or procedure used in civil engineering and chemical engineering to assess the particle size distribution (also called gradation) of a granular material by allowing the material to pass through a series of sieves of progressively smaller mesh size and weighing the amount of material that is stopped by each sieve as a fraction of the whole mass. Shallow size – size of the object itself including only references to other objects, without objects which are referenced. Retained size – size of the object including references to other objects.
Can't find your answer? Please refer to documentation and demos, ask your question in forum, or contact support.
As experience has shown, sometimes a sort of uncertainty may arise on the subject of Java Virtual Machine (JVM) memory structure and other related aspects such as sizes of various kinds of memory, live and dead objects, etc.
In this article, we shall try to illuminate these issues to clear up the point.
Heap and Non-Heap Memory
The JVM memory consists of the following segments:
- Heap Memory, which is the storage for Java objects
- Non-Heap Memory, which is used by Java to store loaded classes and other meta-data
- JVM code itself, JVM internal structures, loaded profiler agent code and data, etc.
Heap
The JVM has a heap that is the runtime data area from which memory for all class instances and arrays are allocated. It is created at the JVM start-up.
The heap size may be configured with the following VM options:
- -Xmx<size> - to set the maximum Java heap size
- -Xms<size> - to set the initial Java heap size
By default, the maximum heap size is 64 Mb.
Heap memory for objects is reclaimed by an automatic memory management system which is known as a garbage collector. The heap may be of a fixed size or may be expanded and shrunk, depending on the garbage collector's strategy.
Non-HeapAlso, the JVM has memory other than the heap, referred to as non-heap memory. It is created at the JVM startup and stores per-class structures such as runtime constant pool, field and method data, and the code for methods and constructors, as well as interned Strings.

Unfortunately, the only information JVM provides on non-heap memory is its overall size. No detailed information on non-heap memory content is available.
The abnormal growth of non-heap memory size may indicate a potential problem, in this case you may check up the following:
- If there are class loading issues such as leaked loaders. In this case, the problem may be solved with the help of Class loaders view.
- If there are strings being massively interned. For detection of such problem, Object allocation recording may be used.
If the application indeed needs that much of non-heap memory and the default maximum size of 64 Mb is not enough, you may enlarge the maximum size with the help of -XX:MaxPermSize
VM option. For example, -XX:MaxPermSize=128m sets the size of 128 Mb.
Heap and non-heap memory usage telemetry is shown in the Memory tab:

Allocated and Used Memory
Allocated and used memory sizes are shown on the graphs for both heap and non-heap.
The allocated memory is an overall amount of memory allocated by the JVM, while used memory is the amount of memory which is actually in use.
Obviously, the allocated memory cannot be less than the used memory. The exact amount of allocated memory is determined by the JVM internal strategies.
Used Heap Memory: Live and Dead Objects
Used heap memory consists of live and dead objects.
Live objects are accessible by the application and will not be a subject of garbage collection.
Dead objects are those which will never be accessible by the application but have not been collected yet by the garbage collector. Such objects occupy the heap memory space until they are eventually collected by the garbage collector.
Note that Class list view in memory telemetry shows both live and dead objects. You may observe the decreasing number of objects when garbage collection occurs automatically or it is forced with the help of the corresponding toolbar button.
Object Sizes in Memory Snapshots: Shallow and Retained Sizes
All individual objects, as well as sets of objects have their shallow and retained sizes.
Shallow size of an object is the amount of allocated memory to store the object itself, not taking into account the referenced objects. Shallow size of a regular (non-array) object depends on the number and types of its fields. Shallow size of an array depends on the array length and the type of its elements (objects, primitive types). Shallow size of a set of objects represents the sum of shallow sizes of all objects in the set.
Retained size of an object is its shallow size plus the shallow sizes of the objects that are accessible, directly or indirectly, only from this object. In other words, the retained size represents the amount of memory that will be freed by the garbage collector when this object is collected. In general, retained size is an integral measure, which helps to understand the structure (clustering) of memory and the dependencies between object subgraphs, as well as find potential roots of those subgraphs.
Dead objects are shown only with shallow size, as they do not actually retain any other objects.
Read more about shallow and retained sizes.
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:
- Shallow Heap
- 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:
Chrome Inspect Shallow Size Vs Retained Size
= C’s shallow heap size + F’s shallow heap size + G’s shallow heap size
= 10 bytes + 10 bytes + 10 bytes
= 30 bytes
Shallow Size Vs Retained Size Calculator
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
= 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
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
Shallow Size Vs Retained Size Formula
= 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.
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,….