performance – Android: what is the differences between shallow and retained heap-ThrowExceptions

Exception or error:

I trying to find out memory leak of app by MAT, Here In list_object I found some numeric value
int Shallow heap and retained heap column. What is these value, and how to know where is memory leak.

How to solve:

From Nikita Salnikov-Tarnovski’s blog:

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. Stay tuned for future
posts on the same topic.

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”.

Now, as we all remember, all Java garbage collection (GC) algorithms
follow this logic:

1)There are some objects which are considered “important” by the GC.
These are called GC roots and are (almost) never discarded. They are,
for example, currently executing method’s local variables and input
parameters, application threads, references from native code and
similar “global” objects.

2)Any objects referenced from those GC roots are assumed to be in use
and hence not discarded by the GC. One object can reference another in
different ways in Java, in the most common case an object A is stored
in a field of an object B. In such case we say “B references A”.

3)The process is repeated until all objects that can be transitively
reached from GC roots are visited and marked as “in use”.

4)Everything else is unused and can be thrown away.


From eclipse memory analyser docs

Shallow vs. Retained Heap

Shallow heap is the memory consumed by one object. An object needs 32
or 64 bits (depending on the OS architecture) per reference, 4 bytes
per Integer, 8 bytes per Long, etc. Depending on the heap dump format
the size may be adjusted (e.g. aligned to 8, etc…) to model better
the real consumption of the VM.

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.

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.
enter image description here

The Minimum Retained Size gives a good (under)estimation of the
retained size which is calculated ways faster than the exact retained
size of a set of objects. It only depends on the number of objects in
the inspected set, not the number of objects in the heap dump.


In Simple words 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.
More Details

Leave a Reply

Your email address will not be published. Required fields are marked *