androidmemory-leaksleakcanary

How to remove memory leaks in android studio using leak canary


This is my leak stack trace from Leak canary in my android app. I was unable to provide the whole stack trace but here are the ends and begining

┬───
│ GC Root: System class
android.app.ActivityThread$ActivityClientRecord instance
│    Leaking: NO (MainActivity↓ is not leaking)
│    activity instance of com.dtech.sellibuy.ui.MainActivity with mDestroyed =
│    false
│    ↓ ActivityThread$ActivityClientRecord.activity
├─ com.dtech.sellibuy.ui.MainActivity instance
│    
│          ~~~
├─ android.widget.FrameLayout instance
│    Leaking: UNKNOWN
│    Retaining 837.2 kB in 24714 objects
│    View not part of a window view hierarchy
│    View.mAttachInfo is null (view detached)
│    View.mID = R.id.null
│    View.mWindowAttachCount = 2
│    mContext instance of dagger.hilt.android.internal.managers.
│    ViewComponentManager$FragmentContextWrapper, wrapping activity com.dtech.
│    sellibuy.ui.MainActivity with mDestroyed = false
│    ↓ ViewGroup.mChildren
│                ~~~~~~~~~
├─ android.view.View[] array
│    Leaking: UNKNOWN
│    Retaining 835.9 kB in 24695 objects
│    ↓ View[0]
│          ~~~
╰→ androidx.constraintlayout.widget.ConstraintLayout instance
​     Leaking: YES (ObjectWatcher was watching this because com.dtech.sellibuy.
​     ui.fragments.nav_fragments.myAds.my_items.MyBooks received
​     Fragment#onDestroyView() callback (references to its views should be
​     cleared to prevent leaks))
​     Retaining 835.8 kB in 24694 objects
​     key = 9b7c80fa-22e4-45b0-b08b-d9d68b28f10e
​     watchDurationMillis = 5716
​     retainedDurationMillis = 716
​     View not part of a window view hierarchy
​     View.mAttachInfo is null (view detached)
​     View.mWindowAttachCount = 2
​     mContext instance of dagger.hilt.android.internal.managers.
​     ViewComponentManager$FragmentContextWrapper, wrapping activity com.dtech.
​     sellibuy.ui.MainActivity with mDestroyed = false

METADATA

Build.VERSION.SDK_INT: 30
Build.MANUFACTURER: samsung
LeakCanary version: 2.8.1
App process name: com.dtech.sellibuy
Stats: LruCache[maxSize=3000,hits=102579,misses=265186,hitRate=27%]
RandomAccess[bytes=12038006,reads=265186,travel=148356067308,range=48451740,size
=61197526]
Analysis duration: 25545 ms

I dont understand the leak or how exactly the leak is happening.It says my layout is leaking memory. Can anyone help me understand the stack trace and how to solve the memory leak.


Solution

  • This was caused by views which were not released in CREATED state during Fragment navigation. See StackOverflow answer Could Navigation Arch Component create a false positive memory leak?