Does SoftReference.recycle () contain in the Bitmap object

If I store bitmaps in a hash map using SoftReference, will SoftReference.recycle () be called in the bitmap? And if this is not so, then what would be the way to correctly clear the bitmap from memory in this situation (when the bitmaps are inside the HashMap)?

+7
source share
4 answers

If I store bitmaps in a hash map using SoftReference, will SoftReference.recycle () be called in the bitmap?

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

You use SoftReference when you want the referenced object to stay alive while the host process is not running in memory. The object will not have the right to collect until the collector wants to free memory. SoftReference said, binding a SoftReference means: "Hook the object until you can no longer." ( link )

You do not need to worry about cleaning the bitmap (calling the recycle method); just let SoftReference do its job.

+5
source

From the Bitmap.recycle document:

 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. 

Thus, for weak hold bitmap is enough. If for some reason you need to aggressively free this resource, you're out of luck with a weak link.

EDIT

I am not familiar with the Bitmap implementation in android, but what can happen causes one to deal with Bitmap resources explicitly, is the fact that some memory is not created on the heap. Thus, the process can end without memory, while there is no need for a GC. Imagine a small object containing a large piece of memory laid out from another place. Finalizing the object can be prepared to free up memory, but there is no reason for the VM for the GC, so its own memory is "lost".

But in this case, a weak link will not help either, since it is processed only after gc. The only thing that helps here is the explicit “recycling”, possibly through reference counting.

+6
source

Bitmap resources will be freed after the GC, thanks to this finalize() method. recycle() consists in freeing resources without waiting for it, if you know that it is no longer needed, which is not your business. You use SoftReference because you want to process the image if there is no memory state.

0
source

If the object referenced by WeakReference is GC'd, I would suggest that this will trigger the recycling method on the bitmap. However, I'm not sure, so in order to be safe, you could do something like overriding the WeakReference class by creating a Bitmap-specific WeakReference class that calls the recycle method when it refrence is GC'd.

There should be something like this in the solution, but it has not been verified:

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

All Articles