BottomSheetDialogFragment 在 LeakCanary 2 中显示内存泄漏,但我不确定为什么?

rin*_*ess 2 memory-leaks leakcanary android-bottomnav

我浏览了许多帖子,但没有找到任何可以帮助我理解 LeakCanary 报告泄漏的原因。我有一个带有 a 的主要活动,com.google.android.material.bottomappbar.BottomAppBar它显示了 a BottomSheetDialogFragment。当您在底部工作表中选择一个项目时,它会更新一些文本,然后关闭对话框片段。现在使用 LeakCanary 运行它会在出现对话框并选择一个项目时显示泄漏。

泄漏看起来像:

    ====================================
    HEAP ANALYSIS RESULT
    ====================================
    1 APPLICATION LEAKS

    References underlined with "~~~" are likely causes.
    Learn more at https://squ.re/leaks.

    152090 bytes retained by leaking objects
    Signature: 3841703253a9bf9893936b1dd318c9dd54bf5a8
    ????
    ? GC Root: System class
    ?
    ?? android.app.ActivityThread class
    ?    Leaking: NO (MainActivity? is not leaking and a class is never leaking)
    ?    ? static ActivityThread.sCurrentActivityThread
    ?? android.app.ActivityThread instance
    ?    Leaking: NO (MainActivity? is not leaking)
    ?    ? ActivityThread.mActivities
    ?? android.util.ArrayMap instance
    ?    Leaking: NO (MainActivity? is not leaking)
    ?    ? ArrayMap.mArray
    ?? java.lang.Object[] array
    ?    Leaking: NO (MainActivity? is not leaking)
    ?    ? Object[].[1]
    ?? android.app.ActivityThread$ActivityClientRecord instance
    ?    Leaking: NO (MainActivity? is not leaking)
    ?    ? ActivityThread$ActivityClientRecord.activity
    ?? com.example.testleak.MainActivity instance
    ?    Leaking: NO (Activity#mDestroyed is false)
    ?    ? MainActivity.bottomNavFragment
    ?                   ~~~~~~~~~~~~~~~~~
    ?? com.example.testleak.BottomNavigationDrawerFragment instance
    ?     Leaking: YES (ObjectWatcher was watching this because com.example.testleak.BottomNavigationDrawerFragment received Fragment#onDestroy() callback and Fragment#mFragmentManager is null)
    ?     key = 74db5e32-a816-418a-9f83-2f50a05f37a4
    ?     watchDurationMillis = 7224
    ?     retainedDurationMillis = 2210
    ?     key = 92744a08-2122-4fbe-9841-08f805fcf6e5
    ?     retainedDurationMillis = 2212
    ====================================
    0 LIBRARY LEAKS

    Library Leaks are leaks coming from the Android Framework or Google libraries.
    ====================================
    METADATA

    Please include this in bug reports and Stack Overflow questions.

    Build.VERSION.SDK_INT: 26
    Build.MANUFACTURER: motorola
    LeakCanary version: 2.1
    App process name: com.example.testleak
    Analysis duration: 4182 ms
    Heap dump file path: /data/user/0/com.example.testleak/files/leakcanary/2020-02-20_10-42-59_515.hprof
    Heap dump timestamp: 1582213384806
    ====================================
Run Code Online (Sandbox Code Playgroud)

所以看起来BottomNavigationDrawerFragment应该在它的onDestroy()方法中清理一些东西。

以下是涉及的主要文件。

主活动.kt

package com.example.testleak

import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import com.example.testleak.BottomNavigationDrawerFragment.OnItemClickListener
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*

class MainActivity : AppCompatActivity() {
    private var bottomNavFragment: BottomNavigationDrawerFragment? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setSupportActionBar(bottom_app_bar)
        bottomNavFragment = BottomNavigationDrawerFragment(clickListener)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        return true
    }

    private var clickListener = object : OnItemClickListener {
        override fun onItemClick(item: Int?) {
            // Based on the item clicked show that fragment
            bottomNavFragment?.dismiss()
            when (item) {
                R.id.nav_item_1 -> {
                    screen_label.text = resources.getString(R.string.item_1)
                }
                R.id.nav_item_2 -> {
                    screen_label.text = resources.getString(R.string.item_2)
                }
                R.id.nav_item_3 -> {
                    screen_label.text = resources.getString(R.string.item_3)
                }
                R.id.nav_item_4 -> {
                    screen_label.text = resources.getString(R.string.item_4)
                }
                R.id.preferences -> {
                    screen_label.text = resources.getString(R.string.preferences)
                }
            }
        }
    }


    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item!!.itemId) {
            android.R.id.home -> {
                bottomNavFragment!!.show(supportFragmentManager, bottomNavFragment!!.tag)
            }
        }
        return true
    }
}
Run Code Online (Sandbox Code Playgroud)

活动_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/content_main" />

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottom_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:backgroundTint="?attr/colorPrimary"
        app:fabAlignmentMode="center"
        app:hideOnScroll="true"
        app:layout_scrollFlags="scroll|enterAlways"
        app:navigationIcon="@drawable/ic_menu_white_24dp"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        style="@style/Widget.MaterialComponents.FloatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_add_circle_outline_white_24dp"
        app:layout_anchor="@id/bottom_app_bar" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/screen_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="120dp"
                android:text="Primary"
                android:textSize="18sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

BottomNavigationDrawerFragment.kt

package com.example.testleak

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.navigation.NavigationView

class BottomNavigationDrawerFragment(private val clickListener: OnItemClickListener) : BottomSheetDialogFragment() {

    lateinit var mapNavView: NavigationView

    interface OnItemClickListener {
        fun onItemClick(item: Int?)
    }

    override fun onDestroy() {
        Log.d("BottomNavFragment", "onDestroy")
        super.onDestroy()
    }

    override fun onDestroyView() {
        Log.d("BottomNavFragment", "onDestroyView")
        super.onDestroyView()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        Log.d("BottomNavFragment", "onCreateView")
        val v = inflater.inflate(R.layout.fragment_bottomsheet, container, false)
        mapNavView = v.findViewById(R.id.nav_view)
        mapNavView.setNavigationItemSelectedListener { menuItem ->
            // Bottom Navigation Drawer menu item clicks
            clickListener.onItemClick(menuItem.itemId)
            true
        }
        return v
    }

}
Run Code Online (Sandbox Code Playgroud)

fragment_bottomsheet.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fitsSystemWindows="true"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">


  <com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:menu="@menu/bottom_nav_drawer_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

bottom_nav_drawer_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <group android:checkableBehavior="none">
    <item
      android:id="@+id/nav_item_1"
      android:title="@string/item_1" />
    <item
      android:id="@+id/nav_item_2"
      android:title="@string/item_2" />
    <item
      android:id="@+id/nav_item_3"
      android:title="@string/item_3" />
    <item
      android:id="@+id/nav_item_4"
      android:title="@string/item_4" />
    <item
      android:id="@+id/preferences"
      android:title="@string/preferences" />
  </group>
</menu>
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激。我确定我忽略了一些东西,但我想我已经看过它很长时间了,我正在看过去。

-Rindress

Pie*_*cau 7

要查看的泄漏跟踪的关键部分是~~~ 在哪里:

?? com.example.testleak.MainActivity instance
?    Leaking: NO (Activity#mDestroyed is false)
?    ? MainActivity.bottomNavFragment
?                   ~~~~~~~~~~~~~~~~~
?? com.example.testleak.BottomNavigationDrawerFragment instance
?     Leaking: YES (ObjectWatcher was watching this because
com.example.testleak.BottomNavigationDrawerFragment received Fragment#onDestroy() callback
and Fragment#mFragmentManager is null)
Run Code Online (Sandbox Code Playgroud)

这告诉我们这MainActivity不是被摧毁,而是BottomNavigationDrawerFragment被摧毁。当BottomNavigationDrawerFragment被销毁时,它应该被垃圾收集。然而,它不能被垃圾收集,因为MainActivity它保留了对它的引用MainActivity.bottomNavFragment

MainActivity.clickListener调用bottomNavFragment?.dismiss()它也应该设置bottomNavFragmentnull。并且不是在 中设置MainActivity.bottomNavFragment新实例,而是MainActivity.onCreate()应在显示片段时创建新实例,例如在MainActivity.onOptionsItemSelected