【问题标题】:leakcanary FirestoreRecyclerAdapter memory leakleakcanary FirestoreRecyclerAdapter 内存泄漏
【发布时间】:2020-03-02 07:08:36
【问题描述】:

我得到了这个从 recyclerview 看来的泄漏金丝雀内存泄漏

D/LeakCanary: ====================================
HEAP ANALYSIS RESULT
====================================
0 APPLICATION LEAKS

References underlined with "~~~" are likely causes.
Learn more at https://squ.re/leaks.
====================================
1 LIBRARY LEAKS

Library Leaks are leaks coming from the Android Framework or Google libraries.

Leak pattern: instance field android.view.ViewGroup$ViewLocationHolder#mRoot
Description: In Android P, ViewLocationHolder has an mRoot field that is not cleared in its clear() method. Introduced in https://github.com/aosp-mirror/platform_frameworks_base/commit/86b326012813f09d8f1de7d6d26c986a909d Bug report: https://issuetracker.google.com/issues/112792715
40271 bytes retained by leaking objects
Signature: 64becd25d6156daa91df6572a75b6a28ddb1
┬───
│ GC Root: System class
│
├─ android.view.ViewGroup$ViewLocationHolder class
│    Leaking: NO (a class is never leaking)
│    ↓ static ViewGroup$ViewLocationHolder.sPool
│                                          ~~~~~
├─ android.util.Pools$SynchronizedPool instance
│    Leaking: UNKNOWN
│    ↓ Pools$SynchronizedPool.mPool
│                             ~~~~~
├─ java.lang.Object[] array
│    Leaking: UNKNOWN
│    ↓ Object[].[0]
│               ~~~
├─ android.view.ViewGroup$ViewLocationHolder instance
│    Leaking: UNKNOWN
│    ↓ ViewGroup$ViewLocationHolder.mRoot
│                                   ~~~~~
├─ androidx.recyclerview.widget.RecyclerView instance
│    Leaking: YES (View.mContext references a destroyed activity)
│    mContext instance of com.android.rangroceryshopping.collaborate.CollaborateActivity with mDestroyed = true
│    View#mParent is set
│    View#mAttachInfo is null (view detached)
│    View.mID = R.id.collaborate_rv
│    View.mWindowAttachCount = 1
│    ↓ RecyclerView.mContext
╰→ com.android.rangroceryshopping.collaborate.CollaborateActivity instance
​     Leaking: YES (ObjectWatcher was watching this because com.android.rangroceryshopping.collaborate.CollaborateActivity received Activity#onDestroy() callback and Activity#mDestroyed is true)
​     key = d9d8e345-f645-4b3c-9f3e-5d5b7fd2cb4b
​     watchDurationMillis = 306199
​     retainedDurationMillis = 301199

====================================
METADATA

Please include this in bug reports and Stack Overflow questions.

Build.VERSION.SDK_INT: 28
Build.MANUFACTURER: samsung
LeakCanary version: 2.2
App process name: com.android.rangroceryshopping
Analysis duration: 18611 ms
Heap dump file path: /data/user/0/com.android.rangroceryshopping/files/leakcanary/2020-03-02_08-45-57_485.hprof
Heap dump timestamp: 1583131584691
====================================

我剥离了适配器,它只包含 FirestoreRecyclerAdapter 扩展相关的变量。没有静态值,没有上下文,没有听众。我在另一个活动中有一个类似的适配器,没有任何问题。

我还尝试将回收器的适配器设置为null,甚至将onStop中的LayoutManager设置为空。仍然没有帮助。

@Override
protected void onStop() {
    super.onStop();
    if (mAdapter != null) {
        mRecyclerView.setLayoutManager( null );
        mRecyclerView.setAdapter( null );
        mAdapter.stopListening();
    }
}

这不可能是 android 泄漏,因为其他 Activity 使用的是相同的,对吧?

在泄漏信息中我看到了这个链接,但我不确定我是否理解它

leakcanary information link

【问题讨论】:

  • 做 mAdapter.stopListening();在 onPause 方法中
  • 它在 onStop 但现在我也尝试了 onPause 和同样的问题
  • 尝试删除所有节点监听
  • 我删除了我可以删除的所有内容,但这是 FirestoreRecyclerAdapter,最后一个对于构造函数是强制性的

标签: android android-recyclerview memory-leaks leakcanary


【解决方案1】:

答案在您粘贴的跟踪的描述中:)

说明:在 Android P 中,ViewLocationHolder 有一个 mRoot 字段,该字段未在其 clear() 方法中清除。引入于https://github.com/aosp-mirror/platform_frameworks_base/commit/86b326012813f09d8f1de7d6d26c986a909d 错误报告:https://issuetracker.google.com/issues/112792715

这是一个已知的 Android 错误。

【讨论】:

  • 感谢您的回答,但我无权访问此链接。几乎每次我进入活动时都会发生这种泄漏,而 GC 有时会变得非常疯狂。没有什么可以解决的吗?可能更多(但不仅仅是)因为我使用的是FirestoreRecyclerAdapter?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多