android - LeakCanary 收到 Fragment#onDestroy() 回调并且 Fragment#mFragmentManager 为空

标签 android memory-leaks leakcanary

我正在尝试使用 LeakCanary 修复内存泄漏,老实说,我无法理解向我显示的堆栈跟踪。我正在使用 firebase 分页选项并停止监听 onDestroy方法。我也没有将任何 Activity 或上下文传递给适配器,所以我确信这没有问题。以下是来自 LeakCanary 的日志。

    HEAP ANALYSIS RESULT
    ====================================
    1 APPLICATION LEAKS
    
    References underlined with "~~~" are likely causes.
    Learn more at https://squ.re/leaks.
    
    31417285 bytes retained by leaking objects
    Signature: 31c372d2f2d3d219e828763d8f853ceca5851b
    ┬───
    │ GC Root: System class
    │
    ├─ leakcanary.internal.InternalLeakCanary class
    │    Leaking: NO (HomeBuyersActivity↓ is not leaking and a class is never leaking)
    │    ↓ static InternalLeakCanary.resumedActivity
    ├─ com.dreamakers.coonna.Activity.HomeBuyersActivity instance
    │    Leaking: NO (Activity#mDestroyed is false)
    │    ↓ HomeBuyersActivity.mLifecycleRegistry
    │                         ~~~~~~~~~~~~~~~~~~
    ├─ androidx.lifecycle.LifecycleRegistry instance
    │    Leaking: UNKNOWN
    │    ↓ LifecycleRegistry.mObserverMap
    │                        ~~~~~~~~~~~~
    ├─ androidx.arch.core.internal.FastSafeIterableMap instance
    │    Leaking: UNKNOWN
    │    ↓ FastSafeIterableMap.mEnd
    │                          ~~~~
    ├─ androidx.arch.core.internal.SafeIterableMap$Entry instance
    │    Leaking: UNKNOWN
    │    ↓ SafeIterableMap$Entry.mKey
    │                            ~~~~
    ├─ com.dreamakers.coonna.Adapter.DiscoverStoreAdapter instance
    │    Leaking: UNKNOWN
    │    ↓ DiscoverStoreAdapter.mParser
    │                           ~~~~~~~
    ├─ com.dreamakers.coonna.Activity.HomeFragment$11 instance
    │    Leaking: UNKNOWN
    │    Anonymous class implementing com.firebase.ui.firestore.SnapshotParser
    │    ↓ HomeFragment$11.this$0
    │                      ~~~~~~
    ╰→ com.dreamakers.coonna.Activity.HomeFragment instance
    ​     Leaking: YES (ObjectWatcher was watching this because com.dreamakers.coonna.Activity.HomeFragment received Fragment#onDestroy() callback and Fragment#mFragmentManager is null)
    ​     key = 6e0451ae-0c0b-4de8-8993-011515e95a80
    ​     watchDurationMillis = 6238
    ​     retainedDurationMillis = 1238
    ====================================
    0 LIBRARY LEAKS
    
    A Library Leak is a leak caused by a known bug in 3rd party code that you do not have control over.
    See https://square.github.io/leakcanary/fundamentals-how-leakcanary-works/#4-categorizing-leaks
    ====================================
    METADATA

最佳答案

HomeFragment 被销毁,应该被垃圾回收,但它不能被垃圾回收,因为 DiscoverStoreAdapter 在 HomeBuyersActivity 上注册为生命周期监听器,并且 DiscoverStoreAdapter 有一个 mParser 字段,它是一个匿名类,在 HomeFragment 中实现了 SnapshotParser。
没有代码很难说,但是当 Fragment 被销毁时,DiscoverStoreAdapter.mParser 可能应该设置为 null。

关于android - LeakCanary 收到 Fragment#onDestroy() 回调并且 Fragment#mFragmentManager 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63260185/

相关文章:

java - 使用 bundle 在 Activity 之间传输数据

.NET RegEx "Memory Leak"调查

android - Leakcanary 与谷歌的 Firebase 崩溃

java - 由于外部库中的 org.junit.Test 类依赖关系,无法使用 LeakCanary

android - 如何在带有模块化代码的 fragment 中使用 LeakCanary 检测内存泄漏?

android - box2d (andengine gles2) PhysicsConnector 语法错误

android - 当键盘弹出时,TextField 被隐藏

c++ - 为什么动态内存在超出范围时不被清理

c++ - 使用 typeinfo::name() 后内存泄漏

java - H2、HSQLDB 或任何其他使用 InputStream 的嵌入式数据库