Android的内存泄漏解决方案 [英] Android Memory Leak solution

查看:365
本文介绍了Android的内存泄漏解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有内存泄漏问题。当我表现出活动的每一件事情的作品。当我preSS,并尝试重新加载活动我得到一个OutOfMemoryException。

I'm having problems with memory leaks. When I show an Activity every thing works. When I press back and try to reload the Activity I get an outOfMemoryException.

所以从我读过,我可以得出结论,在本次活动并非所有的引用被删除,因此该活动不被垃圾收集器回收(因为有活动引用Activitys的arent由GC回收)的文档。

So from the docs I've read I can conclude that in this activity not all references are deleted and so the activity isn't recycled by the garbage collector (because Activitys with active references arent collected by the gc).

例如可以在code以下导致内存泄漏(假设有一个可用少量的内存)?因为我初始化gestureDetector但我从来没有uninitialise是:

For instance could the code below cause a memory leak (Suppose there is a small amount of memory available)? Because I initialise the gestureDetector but I never uninitialise it:

public class FotoGallery    extends Activity    {
    private GestureDetector gestureDetector;
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        gestureDetector = new GestureDetector(new MyGestureDetector());



}
    }

编辑:我已经有这个问题,当我在一个ImageView的图像设置为

edit: I already have this problem when I set an image in an imageView.

public class FotoGallery    extends Activity    {
    private GestureDetector gestureDetector;
    private String path = "/mnt/sdcard/DCIM/img001.jpg";
private Bitmap currentBitmap;


 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        gestureDetector = new GestureDetector(new MyGestureDetector());
        setImage(path);


}

private static final int SWIPE_MIN_DISTANCE = 30;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            // right to left swipe
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                //rightFling detected
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                //leftFling detected
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }

}


private void setImage(String path) {
    if (currentBitmap != null) {
        currentBitmap.recycle();
    }
    final ImageView imageView = (ImageView) findViewById(R.id.imageview);
    currentBitmap = BitmapFactory.decodeFile(path);
    imageView.setImageBitmap(currentBitmap);
}
    }


Now my final question is how can you uninitialise all the variables at the right time? Do you specifically need to listen for when a user pr

S弯回来,然后把所有的变量设置为null?

esses back and then put all variables to null?

推荐答案

什么是您的应用程序(或者说,这个活动)实际上呢?我没有看到,$ C $单独的C怎么会造成多大的危害,但我可能是错的。

What does your application (or rather, this Activity) actually do? I don't see how that code alone could cause much harm, but I could be wrong.

我有一点内存泄漏和放大器麻烦;内存跟踪这是我后来固定的,这些文章给我的洞察力好位:

I had a bit of trouble with memory leaks & memory tracking which I later fixed, these articles gave me a good bit of insight:

HTTP://android-developers.blogspot。 co.uk/2009/01/avoiding-memory-leaks.html

http://android-developers.blogspot.co.uk/2009/02/track-memory-allocations.html

可能需要多一点信息,以帮助你,尽管。您的活动被称为FotoGallery所以我将有一个快速的猜测,说你的问题在​​于,你加载和诸如此类的东西的图片。还有的是相当多的问题,对SO有关有限的内存和放大器;加载图片。

A bit more info may be needed to help you though. Your Activity is called FotoGallery so I'm going to have a quick guess and say your problem lies with the pictures that you load in and whatnot. There's been quite a bit of questions on SO regarding limited memory & loading pictures.

这篇关于Android的内存泄漏解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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