Problem phenomenon
Automatic test feedback a page memory leak phenomenon, almost inevitable, our local debug run found that the problem does exist
This is an anonymous inner class.
Problem location
Fortunately, the memory section of the higher version of Android Studio has been done very well, and we don’t need to convert to hprof format and use MAT analysis as before. We can directly right-click jump to source
A dialog listener is leaking memory. Look at the previous call chain, is it still related to message?
Could it be Android Stuido?
Take a look at the source code:
Huh? Can a setListener operation really be associated with handler and message? It seems that there is no false alarm in Studio
How to solve it?
DismissListener(NULL); dismiss Listener(null); dismiss Listener(null);
Android anonymous inner class memory problem is weak reference
static class CommentDelDialogDismissListener implements DialogInterface.OnDismissListener { private final WeakReference<ForumPostDetailActivity> mActivity; CommentDelDialogDismissListener(ForumPostDetailActivity activity) { mActivity = new WeakReference<>(activity); } @Override public void onDismiss(DialogInterface dialog) { ForumPostDetailActivity activity = mActivity.get(); if (activity == null) { return; } activity.continueVideoPlayerByVisiableRect(); }}Copy the code
This is similar to the standard Static Class CustomHandler method.
The memory leak detected after the run disappeared. Problem solving