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

时间:2019-10-27 21:37:05

标签: memory-leaks android-architecture-components android-viewmodel leakcanary

当我从活动中分离片段时, Leak Canary 检测到androidx.core.widget.NestedScrollView内部的一些内存泄漏。我没有可观察到的东西,只有一个视图模型绑定到了它的数据绑定对象。

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)

1 个答案:

答案 0 :(得分:0)

MoreFragment在onDestroyView之后继续使用FragmentMoreBindingImpl(它本身包含NestedScrollView)。您应该在Fragment.onDestroyView()

中清除对绑定的引用