在Android的回收位图位图、Android

2023-09-05 11:19:19 作者:走了就别回头了

当一个人应该照顾位图的内存管理或回收位图的机器人?

When one should take care of Bitmap memory management or recycling bitmap in android ?

例如,有几种方法可以创建一个位图在Android中像下面

For example , there are few ways to create a bitmaps in android like following

 Bitmap.createBitmap
 Bitmap.createScaledBitmap
 BitmapFactory

但是,当Android的分配内存的位图必须清除,以便在未来的应用中,我们将不会面临内存溢出错误的问题。

But when android allocate memory for bitmap which must be cleared so that in future application we won't face running out of memory error problem

推荐答案

在Android的版本之前的3.0,该位图的虚拟机之外的分配。安卓回来这个内存位图的finalize()方法。你可以让Android的回收内存速度更快通过调用Bitmap.recycle(),而不是等待的GC来调用finalize()在他们身上。

In Android versions prior to 3.0, the Bitmaps are allocated outside of the VM. Android gets back this memory in Bitmap's finalize() method. You can let Android reclaim the memory faster by calling Bitmap.recycle() instead of waiting on the GC to call finalize() on them.

这是唯一一个真正的问题,如果你正在创建并丢弃了大量的位图。也就是说,如果你是分配内存的速度,然后在GC可以清理留下的垃圾,这时你会得到一个OutOfMemoryError。

This is only really a problem if you're creating and discarding a lot of the Bitmaps. That is, if you are allocating memory faster then the GC can clean up the garbage left behind, at which point you get an OutOfMemoryError.

在的Andr​​oid 3.0及更高版本,位图分配内存的虚拟机里面,所以内存的位图可以,而不必调用finalize()对他们的被回​​收。

In android 3.0 and later, the Bitmap memory is allocated inside of the VM, so Bitmap memory can be reclaimed without having to call finalize() on them.