androidperformancememory-managementmemory-leaks

Android: what is the differences between shallow and retained heap


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.


Solution

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