I'm using Eclipse Memory Analyzer, for testing the behavior of MainActivity
in my Android app.
I've rotated my device exactly 10 times, so I have created 11 instances of MainActivity
class.
Looking at the Histogram tab in Eclipse Memory Analyzer, I obtain this
The entire list objects (right click -> lList objects -> with incoming references) give me the list of these 11 objects:
that sholud be right, since I have exactly 11 instances of the MainActivity class as expected (assuming that the GC has not yet released these Activities).
But If I search for "MainActivity" in the dominator_tree tab I obtain only 4 instance (I'm not sure that these are the instance... probably it just show the references active in my application, not all object in the heap):
Where are the other 6 instances? Why these are not shown in the dominator_tree diagram?
Analyzing the results that are shown in the dominator_tree
I understood what happened:
The dominator three shows only the objects that have at least one path from GC root to them. It requires at least one reference for being shown in that diagram (It doesn't metter if is a Weak reference, Soft reference, Strong reference, or Phantom reference).
So, the 6 objects that I can see only in the Histogram
, are not shown in the dominator_tree
diagram since they have no References to them (they are "floating" in the heap).
In particoular one object (obviously the current shown Activity
) is shown in the dominator_tree
diagram since in my application (for testing purpose) I have used a PhantomReference
for traking the GC activity over my objects.
Here is shown one of the objects obtained from the dominator_tree
diagram:
that show a GC root (pr) that is a static variable, which maintains a PhantomReference
to my Activity. In fact the code inside my activity (in the onCreate()
callback) is:
if(savedInstanceState==null)
pr = new PhantomReference(this, q); // q is the ReferenceQueue
It is also interesting to notice that also without my PhantomReference
, the penultimante created Activity is always shown in the dominator_tree
diagram, since it has a Reference
active that starts from the GC root (Native Stack). As you can see below:
It seems like Android uses a sort of cache for the previous instantiated Activity.
Summarizing: The dominator_tree diagram only shows the objects that have at least one path from the GC root to them. While Histogram shows all the objects in the heap memory.