Memory leak when removing ChannelFragment

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!

@RobertM,

First off, welcome to the Sendbird Community. We’re happy you’re here. Let me take a look into this report and see what is going on.

Thanks,
Tyler

1 Like

UPDATE:

I noticed when I open one chat with the ChannelFragment, and then close it and open another, ChannelFragment displays the previous chat info and will display that info with any further chat.
I think this is because the ViewModel is retained for the first chat we opened.

I’m also seeing the same thing. I open a ChannelFragment with one channel url and if I build another channel fragment in the same activity even if I change the channel url it shows the previous channels chat messages. This is currently a show stopper for me in order to use Sendbird UIKit.

1 Like

Thank you for the report @Darin_Tanaka. We are currently awaiting further investigation from our Engineering team.

-Tyler

2 Likes

@RobertM @Darin_Tanaka
Thank you for your reporting!. We found the reason why the memory leak is caused and fixed the issue of using ChannelFragment on the same Activity.

We will release the new version after about 2 weeks. (This is our regular release)

3 Likes

Is there any beta release or any snapshot version so we can test as well?

Thanks for your work!

@RobertM
Sorry, there is no beta or snapshot version…! We test the new version internally and release it.

Hey! Any news on the release?

Thanks

Hi, @tamasimecs new sdk which fixed this will be released within this week at the latest!

UIKit 2.1.0 has been released.!

4 Likes