Android的什么工具和方法的工作最好找内存/资源泄露? [英] What Android tools and methods work best to find memory/resource leaks?

查看:112
本文介绍了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.

什么工具或方法中有你遇到或制定和关怀,在OS项目,也许分享?

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

推荐答案

一个,我发现开发Android应用程序最常见的错误是java.lang.OutOfMemoryError:位图大小超过虚拟经济的错误。我frecuently发现这个错误使用大量位图的改变方向后活动:该活动从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.

位图上的previous活动布局不正确的垃圾收集器释放,因为他们已经越过引用他们的活动。经过多次实验,我发现这个问题的一个比较好的解决方案。

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"
     >
     ...

然后,在你的活动的onDestroy()方法,调用unbindDrawables()方法传递一个refence到父视图,然后做一个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. 将在每个ViewGroup中
  3. 童车

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

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