哪些 Android 工具和方法最适合查找内存/资源泄漏? [英] What Android tools and methods work best to find memory/resource leaks?

查看:18
本文介绍了哪些 Android 工具和方法最适合查找内存/资源泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个 Android 应用程序,我正在开发手机应用程序,一切似乎都运行良好,您想宣布胜利并发布,但您知道只需要一些内存以及那里的资源泄漏;并且在 Android 上只有 16mb 的堆,而且在 Android 应用中显然很容易泄漏.

I've got an Android app developed, and I'm at the point of a phone app development where everything seems to be working well and you want to declare victory and ship, but you know there just have to be some memory and resource leaks in there; and there's only 16mb of heap on the Android and its apparently surprisingly easy to leak in an Android app.

我一直环顾四周,到目前为止只能找到有关hprof"和traceview"的信息,而且都没有得到很多好评.

I've been looking around and so far have only been able to dig up info on 'hprof' and 'traceview' and neither gets a lot of favorable reviews.

您在操作系统项目中遇到或开发过哪些工具或方法并愿意分享?

What tools or methods have you come across or developed and care to share maybe in an OS project?

推荐答案

我发现在开发 Android 应用程序时最常见的错误之一是java.lang.OutOfMemoryError: Bitmap Size Exceeds VM Budget"错误.我经常在更改方向后使用大量位图的活动中发现此错误:活动被销毁,再次创建,并且布局从 XML 中膨胀",消耗了位图可用的 VM 内存.

One of the most common errors that I found developing Android Apps is the "java.lang.OutOfMemoryError: Bitmap Size Exceeds VM Budget" error. I found this error frecuently on activities using lots of bitmaps after changing orientation: the Activity is destroyed, created again and the layouts are "inflated" from the XML consuming the VM memory avaiable for bitmaps.

垃圾收集器未正确释放先前活动布局上的位图,因为它们已交叉引用其活动.经过多次实验,我找到了一个很好的解决方案.

Bitmaps on the previous activity layout are not properly deallocated by the garbage collector because they have crossed references to their activity. After many experiments I found a quite good solution for this problem.

首先,在 XML 布局的父视图上设置id"属性:

First, set the "id" attribute on the parent view of your XML layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:id="@+id/RootView"
     >
     ...

然后,在您的 Activity 的 onDestroy() 方法上,调用 unbindDrawables() 方法,将引用传递给父视图,然后执行 System.gc()

Then, on the onDestroy() method of your Activity, call the unbindDrawables() method passing a refence to the parent View and then do a System.gc()

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

    unbindDrawables(findViewById(R.id.RootView));
    System.gc();
}


private 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();
    }
}

这个 unbindDrawables() 方法递归地探索视图树并且:

This unbindDrawables() method explores the view tree recursively and:

  1. 移除所有后台可绘制对象的回调
  2. 删除每个视图组上的子项

这篇关于哪些 Android 工具和方法最适合查找内存/资源泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆