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 (when 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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With