Android的 - 位图和内存管理?位图、内存管理、Android

2023-09-04 11:05:19 作者:世界留你独自伤悲

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

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

解决方案

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

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

Android 内存管理

有关的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...