Memory leaks 泄漏金丝雀在分离碎片上检测到一些内存泄漏

Memory leaks 泄漏金丝雀在分离碎片上检测到一些内存泄漏,memory-leaks,android-architecture-components,android-viewmodel,leakcanary,Memory Leaks,Android Architecture Components,Android Viewmodel,Leakcanary,当我从活动中分离片段时,Leak Canary在androidx.core.widget.NestedScrollView中检测到一些内存泄漏。我在它里面没有可观察的,只有一个绑定到数据绑定对象的viewmodel ApplicationLeak(className=androidx.core.widget.NestedScrollView, leakTrace= ┬ . . . │ ↓ MainActivity.controller ├─ com.ncapdevi.fragnav.Fra

当我从活动中分离片段时,Leak Canary在androidx.core.widget.NestedScrollView中检测到一些内存泄漏。我在它里面没有可观察的,只有一个绑定到数据绑定对象的viewmodel

ApplicationLeak(className=androidx.core.widget.NestedScrollView, leakTrace=
┬
.
.
.
│    ↓ MainActivity.controller
├─ com.ncapdevi.fragnav.FragNavController
│    Leaking: NO (ArrayList↓ is not leaking)
│    ↓ FragNavController.rootFragments
├─ java.util.ArrayList
│    Leaking: NO (Object[]↓ is not leaking)
│    ↓ ArrayList.elementData
├─ java.lang.Object[]
│    Leaking: NO (MoreFragment↓ is not leaking)
│    ↓ array Object[].[3]
├─ my.package.name.ui.main.more.MoreFragment
│    Leaking: NO (Fragment#mFragmentManager is not null)
│    Fragment.mTag=my.package.name.ui.main.more.MoreFragment4
│    ↓ MoreFragment.binding
│                   ~~~~~~~
├─ my.package.name.databinding.FragmentMoreBindingImpl
│    Leaking: UNKNOWN
│    ↓ FragmentMoreBindingImpl.mRoot
│                              ~~~~~
╰→ androidx.core.widget.NestedScrollView
      Leaking: YES (ObjectWatcher was watching this)
      mContext instance of my.package.name.ui.main.MainActivity with mDestroyed = false
      View#mParent is null
      View#mAttachInfo is null (view detached)
      View.mWindowAttachCount = 1
      key = 2262922d-06f8-4abb-ba7b-d276c6fe3082
      watchDurationMillis = 1910
      retainedDurationMillis = -1
, retainedHeapByteSize=7132)

MoreFragment在onDestroyView之后保留FragmentMoreBindingImpl(它本身保存NestedScrollView)。您应该清除Fragment.onDestroyView()中对绑定的引用。

我如何才能做到这一点?我调用了binding.unbind()(例如,删除其他片段中的所有可观察对象),但内存泄漏仍然存在。尝试设置MoreFragment.binding=null因为Koltin,绑定不是null对象,我将其更改为null-able对象,一个问题消失了。我的适配器有另一个问题,它是由dagger注入的,并且引用了recycler视图。我修复了它,现在所有问题都修复了:D谢谢。很高兴听到这个消息!如果这解决了您的问题,您可以将其标记为;-)