【问题标题】:Memory Leak on DeathMonitor using LeakCanary使用 LeakCanary 的 DeathMonitor 内存泄漏
【发布时间】:2015-05-12 16:27:08
【问题描述】:

我使用 LeakCanary,不幸的是发生了泄漏,这里是 logcat:

In com.appturbo.appoftheday2015:2.09.2:222.
* com.appturbo.appturbo.ui.HomeActivity has leaked:
* GC ROOT com.android.internal.util.AsyncChannel$DeathMonitor.this$0
* references com.android.internal.util.AsyncChannel.mSrcContext
* leaks com.appturbo.appturbo.ui.HomeActivity instance

* Reference Key: e049c2ed-6784-4850-b794-20fa96c13dcf
* Device: motorola google Nexus 6 shamu
* Android Version: 5.1 API: 22
* Durations: watch=5176ms, gc=228ms, heap dump=4974ms, analysis=29320ms

你们中的一些人是否已经看到过这样的泄漏?任何想法?此泄漏出现在:

  • 更改资源配置以切换语言
  • 完成活动
  • 重新开始活动

【问题讨论】:

  • 你能在哪里解决这个问题?
  • 嗨,我已经发布了这个问题的答案。如果您有同样的问题,希望这可以帮助您。

标签: android memory-leaks leakcanary


【解决方案1】:

在挖掘了一些糟糕的内存搜索之后。我找到了解决此内存泄漏的方法。问题是由于一些drawable没有正确地从视图中分离出来,并且在其他一些对象上保留了一个指针。因此 GC 无法删除这些对象,这就是我们的内存泄漏。

为了解决这个问题,我用这段代码取消了drawable的链接。

    public static void unbindDrawables(View view) {
    if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
    }
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
        ((ViewGroup) view).removeAllViews();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 2017-10-26
    • 1970-01-01
    相关资源
    最近更新 更多