I am currently building an android application that will be run only on tablets. One feature is displaying a list of events in a (custom created) dialog.
In some cases, when the user acknowledges these events, the dialog is not beeing garbage collected, and after opening the dialog several times, the number of instances of the dialog that are kept in memory is increasing and increasing.
Since the app is to be run for a long time I guess this will lead to memory issues.
So I took a memory dump and fired up the Eclipse Memory Analyzer (MAT). I never had this kind of problem before and I am not too familiar with MAT but here is what I assume:
I 开发者_C百科can see that there are several instances of my Dialog and some "inner class" (MyDialog$1) still there. For all other "inner classes" (MyDialog$2, MyDialog$3, ...) the count is 0.
Using "Merge shortest paths to GC Roots" with the option "with all references" leads me to android.view.ViewRoot$RunQueue$HandlerAction, so my assumption is that somehow there is a reference of one of my listeners kept?
I hope you can tell me if my assumption (and my way of analyzing) is correct. And hopefully you can give me a solution or hint on how to solve this.
Thanks in advance Sven
I think that you are correct in your assumption. A listener that is an anonymous class will have a reference to the enclosing class. You should unregister the listeners when you are want the dialog destroyed.
Alternatively, you might consider using an API to handle the lifecycle of the Dialog. The deprecated Activity.showDialog
and Activity.removeDialog
should remove all references to the dialog when it is removed.
In Android 3.0 and above you can use the DialogFragment
instead of Dialog
, and the FragmentManager
should handle it's lifecycle for you. See the Fragments developer guide.
精彩评论