【问题标题】:DialogFragment in CustomView is causing memory leakCustomView 中的 DialogFragment 导致内存泄漏
【发布时间】:2020-03-11 14:18:45
【问题描述】:

我有一个 MainActivity,它的布局中有一个 customView(DatePicker)。 DatePicker customView 有一个按钮和一个 CustomDialogFragment。当在 DatePicker 上单击 Button 时,它会显示 CustomDialogFragment。应用程序运行良好,但 leakCanary 显示泄漏。这是代码(为简洁起见,删除了一些代码)

MainActivity.class

class MainActivity : AppCompatActivity() {

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

        date_picker.calendarDialog = getCalendarDialog()
    }

    private fun getCalendarDialog(): CalendarDialog {
        return CalendarDialog()
    }
}

activity_main.xml

  <androidx.constraintlayout.widget.ConstraintLayout>
    <com.example.testproject.customViews.DatePicker
        android:id="@+id/date_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </com.example.testproject.customViews.DatePicker>
 </androidx.constraintlayout.widget.ConstraintLayout>

DatePicker.class

class DatePicker : FrameLayout {
    var calendarDialog: CalendarDialog? = null
    init {
        View.inflate(context, R.layout.date_picker, this)
        open_calendar.setOnClickListener {
            calendarDialog?.show((context as MainActivity).supportFragmentManager.beginTransaction(), "Calendar")
        }
    }
}

CalendarDialog.class

class CalendarDialog: DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val builder = AlertDialog.Builder(context!!)
        builder.setView(view)
            .setMessage("This is a dummy message")
            .setPositiveButton("OK") { dialog, which -> }
            .setNegativeButton("Cancel") { dialog, which -> }

        return builder.create()
    }
}

堆分析结果 ===================================== 1 应用程序泄漏

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

1437 bytes retained by leaking objects
Signature: 1eb8b5c7c3fd403a9a6851729c4044c8a6ce7cf6
┬───
│ GC Root: System class
│
├─ android.view.inputmethod.InputMethodManager class
│    Leaking: NO (InputMethodManager↓ is not leaking and a class is never leaking)
│    ↓ static InputMethodManager.sInstance
├─ android.view.inputmethod.InputMethodManager instance
│    Leaking: NO (DecorView↓ is not leaking and InputMethodManager is a singleton)
│    ↓ InputMethodManager.mNextServedView
├─ com.android.internal.policy.DecorView instance
│    Leaking: NO (LinearLayout↓ is not leaking and View attached)
│    mContext instance of com.android.internal.policy.DecorContext, wrapping activity com.example.testproject.MainActivity with mDestroyed = false
│    Parent android.view.ViewRootImpl not a android.view.View
│    View#mParent is set
│    View#mAttachInfo is not null (view attached)
│    View.mWindowAttachCount = 1
│    ↓ DecorView.mContentRoot
├─ android.widget.LinearLayout instance
│    Leaking: NO (MainActivity↓ is not leaking and View attached)
│    mContext instance of com.example.testproject.MainActivity with mDestroyed = false
│    View.parent com.android.internal.policy.DecorView attached as well
│    View#mParent is set
│    View#mAttachInfo is not null (view attached)
│    View.mWindowAttachCount = 1
│    ↓ LinearLayout.mContext
├─ com.example.testproject.MainActivity instance
│    Leaking: NO (DatePicker↓ is not leaking and Activity#mDestroyed is false)
│    ↓ MainActivity._$_findViewCache
├─ java.util.HashMap instance
│    Leaking: NO (DatePicker↓ is not leaking)
│    ↓ HashMap.table
├─ java.util.HashMap$Node[] array
│    Leaking: NO (DatePicker↓ is not leaking)
│    ↓ HashMap$Node[].[0]
├─ java.util.HashMap$Node instance
│    Leaking: NO (DatePicker↓ is not leaking)
│    ↓ HashMap$Node.value
├─ com.example.testproject.customViews.DatePicker instance
│    Leaking: NO (View attached)
│    mContext instance of com.example.testproject.MainActivity with mDestroyed = false
│    View.parent androidx.constraintlayout.widget.ConstraintLayout attached as well
│    View#mParent is set
│    View#mAttachInfo is not null (view attached)
│    View.mID = R.id.date_picker
│    View.mWindowAttachCount = 1
│    ↓ DatePicker.calendarDialog
│                 ~~~~~~~~~~~~~~
╰→ com.example.testproject.customViews.CalendarDialog instance
​     Leaking: YES (ObjectWatcher was watching this because com.example.testproject.customViews.CalendarDialog received Fragment#onDestroy() callback and Fragment#mFragmentManager is null)
​     key = e176896c-49c6-4b17-a21e-4a6ca7cde260
​     watchDurationMillis = 11213
​     retainedDurationMillis = 6208
​     key = f3a2f22a-c77f-4c8e-a281-d803d110acff
​     watchDurationMillis = 11214
====================================
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: 28
Build.MANUFACTURER: Google
LeakCanary version: 2.2
App process name: com.example.testproject
Analysis duration: 4191 ms
Heap dump file path: /data/user/0/com.example.testproject/files/leakcanary/2020-03-11_10-15-46_729.hprof
Heap dump timestamp: 1583936152876
====================================

这些是我迄今为止尝试过的事情,但没有运气。

  1. 我尝试在不同的地方初始化 CalendarDialog。
  2. 在 CalendarDialog 类中创建一个侦听器,并在对话框关闭时将“calendarDialog”实例设为空。还有更多..

【问题讨论】:

  • 以上类中没有错误代码。你能提供完整的课程代码吗?
  • 我没有更多代码了。就是这样。

标签: android memory-leaks android-dialogfragment leakcanary


【解决方案1】:

您可以将 CalendarDialog 放在 WeakReference 中,这样您就不必通过回调将其显式设置为 null。垃圾收集器会自动清理,避免内存泄漏。

有关 WeakReference 的更多信息:https://developer.android.com/reference/java/lang/ref/WeakReference

【讨论】:

    【解决方案2】:

    当 CalendarDialog 片段被解除时,该片段应该被垃圾回收。但是在这里我们可以看到 DatePicker 布局保留了对它的引用,防止它被垃圾收集。 DatePicker 仍然是附加的,所以它仍然存在是有意义的,但是当对话框被关闭时,它应该将其 DatePicker.calendarDialog 字段设置为 null。

    【讨论】:

    • 我也是这么想的。我在 CalendarDialog 中添加了一个回调函数,并在调用 Dismissed 时将 DatePicker 中的 CalendarDialog 实例设置为 null。这没有帮助。
    【解决方案3】:

    将 CalenderDialog 设为实用函数

    class CalendarDialog {
    
      fun onCreateDialog(context: Context): Dialog {
        val builder = AlertDialog.Builder(context)
        builder
            .setMessage("This is a dummy message")
            .setPositiveButton("OK") { dialog, which -> }
            .setNegativeButton("Cancel") { dialog, which -> }
    
        return builder.create()
      }
    }
    

    class DatePicker(context: Context, attr: AttributeSet) : FrameLayout(context, attr) {
    
      var calendarDialog: CalendarDialog? = null
    
      init {
        View.inflate(context, R.layout.date_picker, this)
        open_calendar.setOnClickListener {
            calendarDialog?.onCreateDialog(context)?.show()
        }
      }
    
      override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        calendarDialog = null
        open_calendar.setOnClickListener(null)
      }
    }
    

    活动不知道对话框片段存在于您的案例中。

    【讨论】:

      【解决方案4】:

      我以前也有过类似的经历。我所做的只是将supportFragmentManager 更改为childFragmentManager。所以我建议改变这个:

      calendarDialog?.show((context as MainActivity).supportFragmentManager.beginTransaction(), "Calendar")
      

      到这里:

      calendarDialog?.show((context as MainActivity).childFragmentManager.beginTransaction(), "Calendar")
      

      另外,寻找任何其他supportFragmentManager(如果有的话)。

      【讨论】:

        猜你喜欢
        • 2016-08-18
        • 2014-08-23
        • 2016-08-20
        • 2015-07-06
        • 2014-06-07
        • 2013-11-20
        • 2011-10-28
        • 2016-01-18
        • 2012-12-13
        相关资源
        最近更新 更多