In a simplified version of my app, I have two activities, A and B. Actvity A starts B, and after some work B calls finish(). Using the Memory Analyzer Tool on most devices (Galaxy Nexus running 4.2, Droid 4 running 4.0.4, and Droid 2 running 2.3.4) shows no trace of activity B, which is what I expected.
But on Samsung S3 running 4.1.1, MAT shows activity B objects still around, due to paths to the following GC roots (weak/soft references excluded):
Class Name | Shallow Heap | Retained Heap
-------------------------------------------------------------------------------------------------
com.myCo.myApp.ActivityB @ 0x42720818 | 264 | 3,280
|- <Java Local> java.lang.Thread @ 0x4271cf60 Thread-21941 Thread| 80 | 52,264
|- mOuterContext android.app.ContextImpl @ 0x426adf68 | 104 | 784
| '- mContext android.media.AudioManager @ 0x428e49a0 | 48 | 152
-------------------------------------------------------------------------------------------------
Every time I start and stop activity B, MAT shows another instance of activity B's memory footprint. And the more I open/close activity B, the bigger the memory footprint reported in logcat. Forcing a GC through MAT does not remove the references to activity B memory.
I have three questions.
Why is the memory/GC behavior different across the different devices?
On S3, will the OS eventually come back and GC the stranded activity B objects (in other words, should I not worry about it because Android will clean it up before a crash)?
If not, where do the Thread and AudioManager references come from, and how would I clear them?
Thanks to any experienced "leak hunters" out there!