查看传呼机内存泄漏使用位图和截击 [英] View Pager Memory Leak with Bitmaps and Volley

查看:189
本文介绍了查看传呼机内存泄漏使用位图和截击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用查看传呼机显示这是从网络中的我的应用程序下载的图像。图像的数量可能会从5到20我用排枪库来做网络运营。该应用程序并没有采取多少内存之前,但现在增加的观点寻呼机之后,应用程序需要大量的内存和每次我打开这个活动,在堆增长(从日志消息选中)使用的内存。我还使用Eclipse的内存分析器来检查哪里漏了,这绝对是位图和本次活动的多个实例。肯定是有泄漏,因为这种活动是没有得到GC'ed,一些引用保持这种距离越来越垃圾收集。我在这里加了我实现了看法寻呼机。

I'm using View Pager to show images which are downloaded from the network in my application. The number of images could be from 5 to 20. I'm using Volley library to do the network operations. The app wasn't taking much memory before but now after adding the view pager, the app takes a lot of memory and every time i open this activity, the memory used in heap increase (checked from the log messages). I also used Eclipse Memory analyzer to check where the leak was and it is definitely the bitmaps and the multiple instances of this activity. There is definitely a leak, as this activity isn't getting GC'ed, some references are keeping this from getting garbage collected. I've added my implementation of the view pager here.

public class ViewPagerAdapter extends PagerAdapter {
        Context context;

        public ViewPagerAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            return photoReferences.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((RelativeLayout) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            final ImageView im;
            final ProgressBar pb;

            View itemView = inflater.inflate(R.layout.place_photos_item, container, false);

            im = (ImageView) itemView.findViewById(R.id.placeImage);
            attributes = (TextView) itemView.findViewById(R.id.placeAttributes);
            pb = (ProgressBar) itemView.findViewById(R.id.progressBarPhoto);

            imageLoader.get(url, new ImageListener() {

                public void onErrorResponse(VolleyError arg0) {
                    im.setImageResource(R.drawable.onErrorImage); 
                }

                public void onResponse(ImageContainer response, boolean arg1) {
                    if (response.getBitmap() != null) {
                        im.startAnimation(AnimationUtils.loadAnimation(context, android.R.anim.fade_in));
                        im.setImageBitmap(response.getBitmap());
                        pb.setVisibility(View.GONE);
                    } 
                }
            });

            ((ViewPager) container).addView(itemView);

            return itemView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((RelativeLayout) object);
        }

    }

此外,screenBytes的我使用的大小的位图缓存3倍的数量(屏幕宽度* screenHeight * 4)。我测试在Nexus 4运行4.3,我从来没有碰到OOM异常导致堆大小是巨大此设备上,但应用程序可以利用的内存超过100 MB(它会崩溃的大多数设备),如果我打开活动一次又一次,在此之前它曾经采取的内存周围16-20 MBS不管。这里的缓存code。

Also, I'm using the Bitmap Cache of size 3 times the number of screenBytes(screenWidth * screenHeight * 4). I'm testing on Nexus 4 running 4.3 and I never run into a OOM exception cause the heap size is huge on this device but the app can take more than 100 mb of memory(it will crash on most devices) if I open the activity again and again, and before it used to take around 16-20 mbs of memory no matter what. Here's the cache code.

public class BitmapCache extends LruCache<Object, Object> implements ImageCache {
        public BitmapCache(int maxSize) {
            super(maxSize);
        }

        @Override
        public Bitmap getBitmap(String url) {
            return (Bitmap) get(url);
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            put(url, bitmap);
        }
    }

任何人都可以请建议我该怎么做才能赶上泄漏?有什么错在查看网页或我的排球使用?我不开心与寻呼机过渡为好,滞后一点,是有关?

Could anyone please suggest me what should I do to catch the leak? Is there anything wrong in the View Pager or my Volley usage? I'm not happy with the transition of the Pager as well, lags a bit, is that related?

更新: 这里是MAT的截图,可能发生泄漏的。这是每一个使用凌空图书馆活动。我一直在阅读了很多,但我也没有解决问题。是凌空造成泄漏或我在做什么可怕的错误?

Update: Here's the screenshot of MAT, possible leak. This is on every activity that uses Volley library. I've been reading a lot but I couldn't solve the problem. Is volley causing leak or am I doing something terribly wrong?

推荐答案

您可以找到您的泄漏使用MAT。首先,你运行你的应用程序,并泄露了一些活动实例。然后你抓堆的快照,并寻找那些泄露的活动对象...您可以使用对象查询语言(OQL)找到它们的类型(例如,SELECT * FROM com.foo.FooActivity)。

You can find your leak by using MAT. First you run your app and leak a few activity instances. Then you grab a snapshot of the heap and look for those leaked Activity objects... you can use 'Object Query Language' (OQL) to find them by type (e.g. "SELECT * FROM com.foo.FooActivity").

一旦你找到了一个泄露的对象,在其上单击鼠标右键,并要求MAT跟踪所有传入的引用回到他们的GC根。泄漏的参考值将是其中的一个。

Once you've found a leaked object, right-click on it and ask MAT to trace all its incoming references back to their GC roots. The leaked reference will be one of those.

为了更好地介绍了技术,你可以尝试这篇文章:

For a better introduction to the technique you could try this article:

http://android-developers.blogspot.co.uk/2011/03/memory-analysis-for-android.html

这篇关于查看传呼机内存泄漏使用位图和截击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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