Android片段管理器内存泄漏

Mo *_*Dev 6 android memory-leaks memory-management leakcanary

我使用LeakCanary检测我的应用程序中的内存泄漏。我的代码有什么问题,为什么会这样?一开始,我将片段管理器作为一个新对象使用,然后我尝试了,getSupportFragmentManager()但同样发生了。

这是日志

    ```
ApplicationLeak(className=com.dev.myApp.ViewDialog, leakTrace=
?
?? android.app.ActivityThread
?    Leaking: NO (ActivityThread? is not leaking and a class is never leaking)
?    GC Root: System class
?    ? static ActivityThread.sCurrentActivityThread
?? android.app.ActivityThread
?    Leaking: NO (ArrayMap? is not leaking)
?    ? ActivityThread.mActivities
?? android.util.ArrayMap
?    Leaking: NO (Object[]? is not leaking)
?    ? ArrayMap.mArray
?? java.lang.Object[]
?    Leaking: NO (ActivityThread$ActivityClientRecord? is not leaking)
?    ? array Object[].[3]
?? android.app.ActivityThread$ActivityClientRecord
?    Leaking: NO (Editor? is not leaking)
?    ? ActivityThread$ActivityClientRecord.activity
?? com.dev.myApp.Editor
?    Leaking: NO (Activity#mDestroyed is false)
?    ? Editor.dialog
?             ~~~~~~
?? com.dev.myApp.ViewDialog
?     Leaking: YES (Fragment#mFragmentManager is null and ObjectWatcher was watching this)
?     key = b2133c35-3af8-4631-81da-f73578e0dd12
?     watchDurationMillis = 62684
?     retainedDurationMillis = 57683
, retainedHeapByteSize=772214)```
Run Code Online (Sandbox Code Playgroud)

这是Java代码

ViewDialog dialog;

public void showMyDialog(int position, String type){
    Bundle bundle = new Bundle();
    bundle.putInt(CONST.POSITION, position);
    bundle.putString(CONST.TYPE, type);
    dialog = new ViewDialog();
    dialog.setArguments(bundle);
    dialog.show(getSupportFragmentManager(), "apps_list");
}

public void hideDialog(){
    if (dialog != null) {
        dialog.dismiss();
    }
}
Run Code Online (Sandbox Code Playgroud)

Pie*_*cau 2

在 hideDialog() 中将 Editor.dialog 设置为 null,以便可以正确地进行垃圾回收。