我关闭任何片段时都会出现未知的内存泄漏。

huangapple go评论86阅读模式
英文:

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

问题

你的应用中出现了内存泄漏问题,即使在关闭任何片段时也会出现。根据你提供的信息,可能是因为在关闭片段时没有正确地清空视图引用所导致的。以下是你提到的一段代码,我将进行翻译:

Fragment blankFragment = new BlankFragment();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.hide(userProfileFragment);     //隐藏当前片段 userProfileFragment
transaction.add(R.id.frame_container, blankFragment,"blank");
transaction.addToBackStack(null);
transaction.commit();

此外,你提到在关闭即使是一个不包含任何视图的 BlankFragment 时仍然出现内存泄漏警告。根据 Leak Canary 给出的信息,问题可能出现在 BlankFragment 实例上,因为它接收到了 Fragment#onDestroy() 回调,且 Fragment#mFragmentManager 为 null。

为了解决内存泄漏问题,你可能需要仔细检查以下几点:

  1. 在片段的生命周期方法中,确保你正确地清空视图引用,尤其是在 onDestroyView() 方法中。这是防止视图引用导致内存泄漏的常用做法。

  2. 确保在关闭片段之前,你取消任何可能仍在执行的异步任务、监听器或回调,以免它们持有对片段的引用。

  3. 考虑使用 FragmentTransaction.remove() 方法来移除片段,而不是隐藏它们,以确保片段及其相关资源得到正确地释放。

  4. BlankFragment 中,也要确保在 onDestroyView() 方法中正确地清空任何可能持有对其他对象的引用的内容。

以上是一些可能有助于解决内存泄漏问题的建议。请根据你的应用情况检查这些方面,并根据需要进行调整。

英文:

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)

答案1

得分: 0

UserProfile_Fragment.transaction 可能是泄漏的主要原因,不应将片段事务保存在内存中,因为它会保留 BlankFragment。

英文:

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

huangapple
  • 本文由 发表于 2020年5月3日 15:32:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/61571034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定