gpt4 book ai didi

Android fragment 管理器内存泄漏

转载 作者:行者123 更新时间:2023-12-04 12:56:42 24 4
gpt4 key购买 nike

我使用 LeakCanary 来检测我的应用程序的内存泄漏。我的代码有什么问题,为什么会这样?一开始我将 fragment 管理器作为一个新对象引用,然后我尝试了 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();
}
}

最佳答案

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

关于Android fragment 管理器内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57521916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com