使用 LeakCanary 在 DeathMonitor 上发生内存泄漏
Memory Leak on DeathMonitor using LeakCanary
我使用 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[=10=]
* 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
你们中的一些人是否已经见过这样的泄漏?任何的想法?此泄漏出现在:
之后
- 更改资源配置以切换语言
- 正在完成 activity
- 正在重新启动 activity
在对一些不好的记忆搜索进行挖掘之后。我找到了解决此内存泄漏的方法。问题是由于某些可绘制对象未正确地从视图分离并将指针保留在其他某个对象上。因此 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();
}
}
我使用 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[=10=]
* 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
你们中的一些人是否已经见过这样的泄漏?任何的想法?此泄漏出现在:
之后- 更改资源配置以切换语言
- 正在完成 activity
- 正在重新启动 activity
在对一些不好的记忆搜索进行挖掘之后。我找到了解决此内存泄漏的方法。问题是由于某些可绘制对象未正确地从视图分离并将指针保留在其他某个对象上。因此 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();
}
}