开发者

Does SoftReference calls .recycle() on Bitmap object

If I store bitmaps in a hashmap using SoftReference, will SoftReference call .recycle() on the Bitmap ? And if it doesn't then what would be a way to clear the bitmap properly from memory under the given situation (w开发者_开发问答hen bitmaps are inside a HashMap) ?


From Bitmap.recycle doc:

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. 

So, to weakly hold the Bitmap is enough. If for some reason you need to agressively free this resource, you're out of luck with a weak reference.

EDIT

I'm not familiar with the Bitmap implementation in android, but what might happen that forces one to deal with Bitmap resources explicitly is the fact that some memory is not created on the heap. So the process may run out of memory while there's no need for GC. Imagine a small object holding a large memory chunk malloced from somewhere else. The finalize of the object may be prepared to free the memory, but there is no reason for the VM to GC, so the native memory is "lost".

But in this case a weak reference will not help either, as it is handled only after a gc. Only thing that helps here is to explicit "recycle" maybe with the help of reference counting.


If I store bitmaps in a hashmap using SoftReference, will SoftReference call .recycle() on the Bitmap ?

No. What if instead of a Bitmap you store a String or a POJO? Do they have the recycle method? Of course no. So, the question is: what's SoftReference for?

You use a SoftReference when you want the referenced object to stay alive until the host process is running low on memory. The object will not be eligible for collection until the collector needs to free memory. Loosely stated, binding a SoftReference means, "Pin the object until you can't anymore." (link)

You don't have to care about clearing the Bitmap (invoking the recycle method); just let the SoftReference do its work.


Bitmap's associated resources will be freed upon GC, thanks to it's finalize() method. recycle() is to free the resources without waiting for it, if you know it is no longer needed, which is not your case. You use SoftReference because you want to recycle the image if there is out of memory condition.


If the object referenced by the WeakReference will be GC'd, I'd assume this whill trigger the recycle method on the bitmap. However I'm not sure, so just to be on the safe side you could do something like overriding the WeakReference class, making a Bitmap-specific WeakReference class that calls the recycle method when it's refrence is GC'd.

The solution should ook something like this, but it's untested:

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();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜