Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (when bitmaps are inside a HashMap) ?

like image 517
2cupsOfTech Avatar asked Jan 02 '11 19:01

2cupsOfTech


1 Answers

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.

like image 119
mtraut Avatar answered Sep 19 '22 05:09

mtraut