我使用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)```
这是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();
}
}
答案 0 :(得分:0)
在hideDialog()中将Editor.dialog设置为null,以便可以正确地对其进行垃圾收集。