微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

BottomSheetBehavior泄漏片段?

如何解决BottomSheetBehavior泄漏片段?

我正在使用LeakCanary 2.5分析我的应用程序。

我很难找出我在测试期间发现的最后一次内存泄漏之一。我真的不明白我在做什么错。

LeakCanary日志如下:

图片

LeakCanary Java Memory Dump Image

日志:

┬───
│ GC Root: Global variable in native code
│
├─ myApp.view.MyView instance
│    Leaking: NO (MainActivity↓ is not leaking and View attached)
│    View is part of a window view hierarchy
│    View.mAttachInfo is not null (view attached)
│    View.mID = R.id.my_view
│    View.mWindowAttachCount = 1
│    mContext instance of myApp.activity.MainActivity with mDestroyed
│    = false
│    ↓ MyView.mContext
├─ myApp.activity.MainActivity instance
│    Leaking: NO (ConstraintLayout↓ is not leaking and Activity#mDestroyed is
│    false)
│    mApplication instance of androidx.multidex.MultiDexApplication
│    mBase instance of androidx.appcompat.view.ContextThemeWrapper,not
│    wrapping kNown Android context
│    ↓ MainActivity._bottomSheet
├─ androidx.constraintlayout.widget.ConstraintLayout instance
│    Leaking: NO (View attached)
│    View is part of a window view hierarchy
│    View.mAttachInfo is not null (view attached)
│    View.mID = R.id.bottom_sheet
│    View.mWindowAttachCount = 1
│    mContext instance of myApp.activity.MainActivity with mDestroyed
│    = false
│    ↓ ConstraintLayout.mKeyedTags
│                       ~~~~~~~~~~
├─ android.util.SparseArray instance
│    Leaking: UNKNowN
│    Retaining 2427 bytes in 83 objects
│    ↓ SparseArray.mValues
│                  ~~~~~~~
├─ java.lang.Object[] array
│    Leaking: UNKNowN
│    Retaining 2394 bytes in 81 objects
│    ↓ Object[].[1]
│               ~~~
╰→ myApp.fragment.MetadataFragment instance
     Leaking: YES (ObjectWatcher was watching this because myApp.
     fragment.MetadataFragment received Fragment#onDestroy() callback and
     Fragment#mFragmentManager is null)
     Retaining 2258 bytes in 75 objects
     key = 0409b825-f199-4049-95b1-8e572c92a078
     watchDurationMillis = 6335
     retainedDurationMillis = 1335

最下面的XML定义如下:

<androidx.constraintlayout.widget.ConstraintLayout
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/bottom_sheet"
  android:layout_width="match_parent"
  android:layout_height="100dp"
  android:background="?attr/bottom_sheet_background"
  app:behavior_hideable="false"
  app:behavior_peekHeight="55dp"
  app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

如日志所示,我在MainActivity中存储了_bottomSheet的引用。我以编程方式获得了像这样的BottomSheetBehavior.from(_bottomSheet);的BottomSheetBehavIoUr来附加一个addBottomSheetCallback

在活动的onDestroy中,我认为自己正在清理,但显然缺少某些内容

@Override
protected void onDestroy()
{
  super.onDestroy();
  ...

  _bottomSheetBehavior.removeBottomSheetCallback(_bottomSheetBehaviorCallBack);
  _bottomSheet = null;

  ...
}

我想我在LeakCanary日志中看到的是ConstraintLayout持有MetadataFragment标签。我发现变量LeakCanary在Android(mKeyedTags-> View)的ConstraintLayout extends ViewGroup类中是关于ViewGroup extends View的。

我已经检查了我的代码和XML,并且没有使用标签。我在这里想念什么?

Android View class

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。