Search code examples
androidandroid-recyclerviewmemory-leaksleakcanary

leakcanary FirestoreRecyclerAdapter memory leak


I am getting this leak canary memory leak which seems from the 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
====================================

I stripped the adapter, it only mostly contains FirestoreRecyclerAdapter extension related variables. No static values, no context, no listeners. I have a similar adapter in another activity without any issues.

I also tried to set the adapter of the recycler to null and even the LayoutManager in the onStop. still doesn't help.

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

It can't be an android leak since other activities are using the same, right?

At the leak information i see this links but i am not sure i understand it

leakcanary information link


Solution

  • The answer is in the description of the trace you pasted :)

    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

    This is a known Android bug.