释放内存在的GridView [英] Free up memory in GridView

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

问题描述

对于应用我发展,我想填充大量的图像的GridView。为了避免OutOfMemoryExceptions,我检查可用内存量,并在达到一定的阈值时,我尝试以释放内存就像这样:

For an app I'm developing, I am trying to populate a GridView with a lot of images. To avoid OutOfMemoryExceptions, I check the amount of available memory and when a certain threshold is reached, I try to free up memory like so:

private void freeUpMemory() {
    // Clear ImageViews up to current position
    for (int i = 0; i < mCurrentPosition; i++) {
        RelativeLayout gridViewElement = (RelativeLayout) mGridView.getChildAt(i);
        if (gridViewElement != null) {
            ImageView imageView = (ImageView) gridViewElement.findViewById(R.id.image);
            imageView.getDrawable().setCallback(null);
            imageView = null;
        }
    }
}

我注意到,这实际上并没有释放内存。我不知道就是为什么。我缺少的东西吗?

I noticed that this does not actually free up memory. What I don't know is why. Am I missing something?

推荐答案

当您ImageAdapter得到getView()回调与convertView不为空,它是告诉你,由ImageAdapter pviously提供的这一观点$ P $无在屏幕上不再可见。这是恢复视图使用的资源的好时机。沿着线的东西:

When your ImageAdapter gets the "getView()" callback with convertView not null, it is telling you that this view previously supplied by the ImageAdapter is no longer visible on the screen. That's a good time to recover the resources used by the view. Something along the lines of:

 ImageView iv = (ImageView)convertView.findViewById(R.id.image_view_in_grid_item);
 iv.setDrawable(null);

应删除引用,该被存储在的ImageView的绘制对象。如果在你的code到任何其它引用可绘制它应该是进行垃圾回收。

should remove the reference to the Drawable that is stored in the ImageView. If there are no other references in your code to that Drawable it should be available for garbage collection.

更重要的是,如果你有其他图像显示。

Better yet, if you have another image to be displayed.

iv.setDrawable(newImage);

然后返回convertView作为新的视图由网格被用于
将用新的替换旧的绘制对象,删除了参考和潜在的垃圾收集图像。

Then returning convertView as the new view to be used by the grid will replace the old Drawable with a new one, removing the reference and potentially garbage collecting the image.

这篇关于释放内存在的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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