如果我使用SoftReference在HashMap中存储位图,SoftReference会调用Bitmap的.recycle()方法吗?如果不会,那么在给定情况下(当位图在HashMap中)清除位图的正确方法是什么?
来自 Bitmap.recycle 文档:
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.
Bitmap的相关资源将在GC时通过它的finalize()
方法被释放。如果您知道Bitmap不再需要,则可以使用recycle()
方法立即释放资源,但这不是您的情况。您使用SoftReference
因为您想在内存不足的情况下回收图像。
private final class WeakBitmapReference extends WeakReference<Bitmap> {
public WeakBitmapReference(Bitmap b) {
super(b);
}
public void clear() {
Bitmap b = get();
if (b != null && !b.isRecycled())
b.recycle();
super.clear();
}
}