I'm analyzing my app using LeakCanary 2.5.
I've a hard time to figure out one of the last memory leaks I found during testing. I don't really get what I'm doing wrong here.
The LeakCanary log looks like this:
Image:
Log:
┬───
│ GC Root: Global variable in native code
│
├─ myApp.view.MyView instance
│ Leaking: NO (MainActivity↓ is not leaking and View attached)
│ View is part of a window view hierarchy
│ View.mAttachInfo is not null (view attached)
│ View.mID = R.id.my_view
│ View.mWindowAttachCount = 1
│ mContext instance of myApp.activity.MainActivity with mDestroyed
│ = false
│ ↓ MyView.mContext
├─ myApp.activity.MainActivity instance
│ Leaking: NO (ConstraintLayout↓ is not leaking and Activity#mDestroyed is
│ false)
│ mApplication instance of androidx.multidex.MultiDexApplication
│ mBase instance of androidx.appcompat.view.ContextThemeWrapper, not
│ wrapping known Android context
│ ↓ MainActivity._bottomSheet
├─ androidx.constraintlayout.widget.ConstraintLayout instance
│ Leaking: NO (View attached)
│ View is part of a window view hierarchy
│ View.mAttachInfo is not null (view attached)
│ View.mID = R.id.bottom_sheet
│ View.mWindowAttachCount = 1
│ mContext instance of myApp.activity.MainActivity with mDestroyed
│ = false
│ ↓ ConstraintLayout.mKeyedTags
│ ~~~~~~~~~~
├─ android.util.SparseArray instance
│ Leaking: UNKNOWN
│ Retaining 2427 bytes in 83 objects
│ ↓ SparseArray.mValues
│ ~~~~~~~
├─ java.lang.Object[] array
│ Leaking: UNKNOWN
│ Retaining 2394 bytes in 81 objects
│ ↓ Object[].[1]
│ ~~~
╰→ myApp.fragment.MetadataFragment instance
Leaking: YES (ObjectWatcher was watching this because myApp.
fragment.MetadataFragment received Fragment#onDestroy() callback and
Fragment#mFragmentManager is null)
Retaining 2258 bytes in 75 objects
key = 0409b825-f199-4049-95b1-8e572c92a078
watchDurationMillis = 6335
retainedDurationMillis = 1335
The bottom sheet XML definition looks like this:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="?attr/bottom_sheet_background"
app:behavior_hideable="false"
app:behavior_peekHeight="55dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
As the log shows I store a reference of _bottomSheet
in my MainActivity. I programmatically get the BottomSheetBehaviour like that BottomSheetBehavior.from(_bottomSheet);
to attach a addBottomSheetCallback
.
In my activity's onDestroy
I think I clean up but obviously something is missing:
@Override
protected void onDestroy()
{
super.onDestroy();
...
_bottomSheetBehavior.removeBottomSheetCallback(_bottomSheetBehaviorCallBack);
_bottomSheet = null;
...
}
What I think I read in the LeakCanary log is that the ConstraintLayout
holding a Tag to the MetadataFragment
. I found the variable LeakCanary is referring about mKeyedTags
in the View
class of Android (ConstraintLayout extends ViewGroup
-> ViewGroup extends View
).
I've checked my code and my XML and I don't use Tags. What do I miss here?