Bitmap recycle方法与制作Bitmap的内存缓存
recycle方法不是必须调用,就算调了GC也不会立即回收Java层的Bitmap对象。这个跟把一个对象手动置空一个道理。可以看一下API说明:
This operation cannot be reversed, so it should only be called if you are sure there are no further uses for the bitmap. This is an advanced call, and normally need not be called, since the normal GC process will free up this memory when there are no more references to this bitmap.
那么为什么程序中一般还需要调用呢?我们可以看一下recycle方法到底干了什么,下面是它的调用链:
Bitmap.recycle()(java)->nativeRecycle(mNativeBitmap)(java native)->SKBitmap::setPixels(NULL, NULL)(C++)->freePixels()(C++)
可以看出,recycle方法就是把C层的像素内存给释放了。我们知道,C层的内存还是算到当前进程的内存里的,而构成一个Bitmap对象的内存大部分都是C层的像素数组。所以,手动调用Bitmap.recycle并把Bitmap对象的Java层引用手动置NULL可以瞬间释放像素所占的内存,并让虚拟机下次运行时回收Bitmap对象所占的内存。不要忘了,因为包含了一堆的像素数据,Bitmap对象通常都很大。当很容易判断出这个图片不再被使用的时候就把它recycle一下吧。
但有一种情况比较除外,这就是程序中的列表界面,因为你不能判断用户会滚上还是滚下,而列表又比较长你不能保持所有的列表项中的图片。怎么办呢?
先说一个小测试:
for (int i = 0; i < 1024; i++) {Bitmap b = Bitmap.createBitmap(1024*4, 1024*2, Config.ARGB_8888);}