I’m using SendBird UIKit for Android. I only need the chat UI already created by you guys, so I created a ChannelFragment using its Builder pattern. When I remove it from view ( either by FragmentTransaction -> remove or by getSupportFragmentManager().popBackStackImmediate();
) I get an instant memory leak by LeakCanary.
Apparently is because ChannelViewModel remains in memory when onDestroy gets called.
This is how I add the fragment:
ChannelFragment channelFragment = new ChannelFragment.Builder(mChannelUrl)
.setUseHeader(false)
.setUseLastSeenAt(false)
.setUseTypingIndicator(true)
.build();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.frame_layout, channelFragment, "ChannelFragment").commit();
I have the databinding on my app build.gradle like:
dataBinding { enabled = true }
And R.id.frame_layout
is in my activity layout.
I’m using this library version:
implementation 'com.sendbird.sdk:uikit:2.0.2'
Here is the Heap Dump by LeakCanary:
┬───
│ GC Root: System class
│
├─ android.view.inputmethod.InputMethodManager class
│ Leaking: NO (InputMethodManager↓ is not leaking and a class is never
│ leaking)
│ ↓ static InputMethodManager.sInstance
├─ android.view.inputmethod.InputMethodManager instance
│ Leaking: NO (DecorView↓ is not leaking and InputMethodManager is a
│ singleton)
│ ↓ InputMethodManager.mCurRootView
├─ com.android.internal.policy.DecorView instance
│ Leaking: NO (PagerRecyclerView↓ is not leaking and View attached)
│ View is part of a window view hierarchy
│ View.mAttachInfo is not null (view attached)
│ View.mWindowAttachCount = 1
│ mContext instance of com.android.internal.policy.DecorContext, wrapping
│ activity com.test.nm.view.StartupActivity with mDestroyed = false
│ ↓ View.mAttachInfo
├─ android.view.View$AttachInfo instance
│ Leaking: NO (PagerRecyclerView↓ is not leaking)
│ ↓ View$AttachInfo.mScrollContainers
├─ java.util.ArrayList instance
│ Leaking: NO (PagerRecyclerView↓ is not leaking)
│ ↓ ArrayList.elementData
├─ java.lang.Object array
│ Leaking: NO (PagerRecyclerView↓ is not leaking)
│ ↓ Object.[2]
├─ com.sendbird.uikit.widgets.PagerRecyclerView instance
│ Leaking: NO (View attached)
│ View is part of a window view hierarchy
│ View.mAttachInfo is not null (view attached)
│ View.mID = R.id.rvMessageList
│ View.mWindowAttachCount = 1
│ mContext instance of com.test.nm.view.StartupActivity with mDestroyed
│ = false
│ ↓ PagerRecyclerView.snappableScrollListener
│ ~~~~~~~~~~~~~~~~~~~~~~~
├─ com.sendbird.uikit.widgets.PagerRecyclerView$SnappableScrollListener instance
│ Leaking: UNKNOWN
│ Retaining 36 B in 2 objects
│ ↓ PagerRecyclerView$SnappableScrollListener.pager
│ ~~~~~
├─ com.sendbird.uikit.vm.ChannelViewModel instance
│ Leaking: UNKNOWN
│ Retaining 1.1 MB in 7140 objects
│ ↓ BaseViewModel.provider
│ ~~~~~~~~
╰→ com.sendbird.uikit.fragments.ChannelFragment instance
Leaking: YES (ObjectWatcher was watching this because com.sendbird.uikit.
fragments.ChannelFragment received Fragment#onDestroy() callback and
Fragment#mFragmentManager is null)
Retaining 1.1 MB in 7079 objects
key = 50884f5a-d36b-4943-9284-7c5d8385a275
watchDurationMillis = 12884
retainedDurationMillis = 7865
METADATA
Build.VERSION.SDK_INT: 29
Build.MANUFACTURER: Google
LeakCanary version: 2.6
Stats: LruCache[maxSize=3000,hits=6225,misses=80866,hitRate=7%]
RandomAccess[bytes=4472126,reads=80866,travel=24959464795,range=18149443,size=22
958525]
Heap dump reason: user request
Analysis duration: 6158 ms
I appreciate in advance your help!