调用lifecycle.addObserver后出现内存泄漏

时间:2019-05-09 09:23:51

标签: android memory-leaks leakcanary

从Android jetpack arch组件添加了生命周期观察器之后,我得到了以下泄漏。有什么想法吗?

LeakingInstance(referenceKey=3f842e15-cca3-465a-8345-3a241c8befca, referenceName=, instanceClassName=androidx.navigation.fragment.NavHostFragment, watchDurationMillis=561193, excludedLeak=false, leakTrace=
┬
├─ android.view.inputmethod.InputMethodManager$1
│    Leaking: NO (it's a GC root)
│    Anonymous subclass of com.android.internal.view.IInputMethodClient$Stub
│    ↓ InputMethodManager$1.this$0
│                           ~~~~~~
├─ android.view.inputmethod.InputMethodManager
│    Leaking: UNKNOWN
│    ↓ InputMethodManager.mImeInsetsConsumer
│                         ~~~~~~~~~~~~~~~~~~
├─ android.view.ImeInsetsSourceConsumer
│    Leaking: UNKNOWN
│    ↓ ImeInsetsSourceConsumer.mController
│                              ~~~~~~~~~~~
├─ android.view.InsetsController
│    Leaking: UNKNOWN
│    ↓ InsetsController.mViewRoot
│                       ~~~~~~~~~
├─ android.view.ViewRootImpl
│    Leaking: UNKNOWN
│    ↓ ViewRootImpl.mActivityConfigCallback
│                   ~~~~~~~~~~~~~~~~~~~~~~~
├─ android.app.-$$Lambda$ActivityThread$ActivityClientRecord$HOrG1qglSjSUHSjKBn2rXtX0gGg
│    Leaking: UNKNOWN
│    ↓ -$$Lambda$ActivityThread$ActivityClientRecord$HOrG1qglSjSUHSjKBn2rXtX0gGg.f$0
│                                                                                ~~~
├─ android.app.ActivityThread$ActivityClientRecord
│    Leaking: UNKNOWN
│    ↓ ActivityThread$ActivityClientRecord.activity
│                                          ~~~~~~~~
├─ com.x.x.MainActivity
│    Leaking: YES (Activity#mDestroyed is true)
│    ↓ MainActivity.mFragments
├─ androidx.fragment.app.FragmentController
│    Leaking: YES (MainActivity↑ is leaking)
│    ↓ FragmentController.mHost
├─ androidx.fragment.app.FragmentActivity$HostCallbacks
│    Leaking: YES (FragmentController↑ is leaking)
│    ↓ FragmentActivity$HostCallbacks.mFragmentManager
├─ androidx.fragment.app.FragmentManagerImpl
│    Leaking: YES (FragmentActivity$HostCallbacks↑ is leaking)
│    ↓ FragmentManagerImpl.mPrimaryNav
╰→ androidx.navigation.fragment.NavHostFragment
​     Leaking: YES (Fragment#mFragmentManager is null)
, retainedHeapSize=null)```

2 个答案:

答案 0 :(得分:1)

这是Q Beta上Android框架中的一个已知IME泄漏。在最新版本中应标识为已知泄漏:https://github.com/square/leakcanary/blob/master/leakcanary-analyzer/src/main/java/leakcanary/AndroidKnownReference.kt#L312

答案 1 :(得分:0)

有不同的生命周期观察者状态,例如@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)或@OnLifecycleEvent(Lifecycle.Event.ON_RESUME),可以在onPause生命周期方法中删除侦听器或接收器,然后再次在onResume生命周期方法中初始化

请使用下面的链接详细了解生命周期感知组件及其生命周期

Lifecycler Observer Documentation