当我从活动中分离片段时, 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)
答案 0 :(得分:0)
MoreFragment在onDestroyView之后继续使用FragmentMoreBindingImpl(它本身包含NestedScrollView)。您应该在Fragment.onDestroyView()
中清除对绑定的引用