Android的 - 位图和内存管理? [英] Android - Bitmap and memory management?

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

问题描述

我已经看到了很多样品,开发人员调用循环()的位图,然后将其设置为。 为什么这是必要的,没有垃圾收集器采取释放位图的照顾?

 位图位= BitmapFactory.de codeStream(InputStream的);
bitmap.recycle();
位= NULL;
 

解决方案

加入俱乐部。那种它,但不完全是。

的事情是,在pre-蜂窝版本的Andr​​oid位图的记忆是(被)分配从非托管内存,这就造成各种问题。它仍然是释放,但与位图对象实施的终结。这意味着它至少需要2通行证GC来收集。此外,如果由于某种原因终结执行失败 - 你得到的图片。另一件事是 - 这是真的无从考证 - DDMS不会看到它,同样没有MAT

有关的Andr​​oid 3.0这已被更改,位图在管理字节数组实现的,但是对于上了年纪的手机...

I've seen in a lot of samples, that developers call recycle() on bitmap, and then set it to null. Why is this necessary, doesn't the garbage collector take care of releasing the bitmap?

Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
bitmap.recycle();
bitmap = null;

解决方案

Join the club. It kind of does but not quite.

The thing is that in the pre-Honeycomb versions of Android the memory for bitmaps was (is) allocated from unmanaged memory, which creates all sorts of problems. It is still released but from the finalizer of the bitmap object implementation. Which means that it will take at least 2 passes of GC to collect it. Also if for whatever reason the finalizer fails to execute - you got the picture. Another thing is - it is really difficult to trace - DDMS does not see it and neither does MAT

For Android 3.0 this has been changed and bitmaps are implemented over managed byte arrays, but for the older phones...

这篇关于Android的 - 位图和内存管理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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