简体   繁体   中英

I am getting unknown memory leak whenever I am closing any fragment

I am making blog app and getting memory leak when ever I closing any fragment....I try to find answer and some says to null out your views reference on onDestroyView or onDestroy(I tried it both and it don't work) and than I just created blank fragment without any views(just a blue background ) and even if I am closing BlankFragment,still I am getting same memory leak...follow is the code I am using to add blank fragment

 Fragment blankFragment = new BlankFragment();
 FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
 transaction.hide(userProfileFragment);     //hiding userProfileFragment which is current fragment
 transaction.add(R.id.frame_container, blankFragment,"blank");
 transaction.addToBackStack(null);
 transaction.commit();

and after I am closing just even a blankFragment which does not hold any views..I am getting the following memory leak warning in Leak Canary

┬───
│ GC Root: System class
│
├─ leakcanary.internal.InternalLeakCanary class
│    Leaking: NO (MainActivity↓ is not leaking and a class is never leaking)
│    ↓ static InternalLeakCanary.resumedActivity
├─ MainActivity instance
│    Leaking: NO (HomeFragment↓ is not leaking and Activity#mDestroyed is false)
│    ↓ MainActivity.startingFragment
├─ HomeFragment instance
│    Leaking: NO (UserProfile_Fragment↓ is not leaking and Fragment#mFragmentManager is not        null)
│    Fragment.mTag=home
│    ↓ HomeFragment.mFragmentManager
├─ androidx.fragment.app.FragmentManagerImpl instance
│    Leaking: NO (UserProfile_Fragment↓ is not leaking)
│    ↓ FragmentManagerImpl.mAdded
├─ java.util.ArrayList instance
│    Leaking: NO (UserProfile_Fragment↓ is not leaking)
│    ↓ ArrayList.elementData
├─ java.lang.Object[] array
│    Leaking: NO (UserProfile_Fragment↓ is not leaking)
│    ↓ Object[].[3]
├─ UserProfile_Fragment instance
│    Leaking: NO (Fragment#mFragmentManager is not null)
│    Fragment.mTag=user
│    ↓ UserProfile_Fragment.transaction
│                           ~~~~~~~~~~~
├─ androidx.fragment.app.BackStackRecord instance
│    Leaking: UNKNOWN
│    ↓ BackStackRecord.mOps
│                      ~~~~
├─ java.util.ArrayList instance
│    Leaking: UNKNOWN
│    ↓ ArrayList.elementData
│                ~~~~~~~~~~~
├─ java.lang.Object[] array
│    Leaking: UNKNOWN
│    ↓ Object[].[1]
│               ~~~
├─ androidx.fragment.app.FragmentTransaction$Op instance
│    Leaking: UNKNOWN
│    ↓ FragmentTransaction$Op.mFragment
│                             ~~~~~~~~~
╰→ BlankFragment instance Leaking: YES (ObjectWatcher was watching this because BlankFragment   received Fragment#onDestroy() callback and Fragment#mFragmentManager is null)

UserProfile_Fragment.transaction is the likely cause of the leak, you shouldn't hold the fragment transaction in memory as it keeps the BlankFragment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM