Search This Blog

Friday 26 October 2012

Difference between Weak reference and Soft reference in android and Java as well

In java as well as android, two concept are highly important but people are not much aware about these concept. These are weak reference and soft reference. They ensure to avoid memory over flow error and ensure fast caching of images. By name, They both seems similar but had lots of difference  If i say they are just opposite to each other , then its true.

Weak references : Weak reference is reference to an object that does not put strong force to remain object in memory. It allow garbage collector to collect object that it pointing. Take an example of creating Weakreference

      HashMap<String, WeakReference<Bitmap>> cache = new HashMap<String, WeakReference<Bitmap>>();

 cache.get(key) will return a Bitmap object that is pointed by Weakreference . As we know it does not put much force to keep this reference So you never know when this Bitmap will garbage collected and it starts returning null. Weak reference avoid memory overflow error by allowing garbage collector to collect its object without any algorithm

Where its Useful ?

Now the question arise when to use Weakreference,where memory reclaim highly needed. In simple case,avoiding Weakreference is recommended.

Soft Reference : Soft reference provide a strong reference to an object so that i will garbage collected as late as possible . Lets create one example
               private HashMap<String, SoftReference<Bitmap>> cache = new HashMap<String, SoftReference<Bitmap>>();

Soft Reference is helpful while you want to cache images to avoid time frame that consume in loading from Disk every time. It will make navigation fast and interactive. cache.get(key) will not return null as late as possible. It will garbage collected when VM running out memory.

Mechanism for garbage collection of soft reference from android developer official site

The system may delay clearing and en-queue soft references, yet all SoftReferences pointing to softly reachable objects will be cleared before the run time throws an   OutOfMemoryError.

Unlike a WeakReference, a SoftReference will not be cleared and en-queue until the run time must reclaim memory to satisfy an allocation.

From Oracle About Soft reference-

All soft references to softly-reachable objects are guaranteed to have been cleared before the virtual machine throws an OutOfMemoryError. Otherwise no constraints are placed upon the time at which a soft reference will be cleared or the order in which a set of such references to different objects will be cleared. Virtual machine implementations are, however, encouraged to bias against clearing recently-created or recently-used soft references.


  •     SoftReference should be cleared and en-queue as late as possible, that is, in case the VM is in danger of running out of memory.

  •    WeakReference may be cleared and en-queue as soon as is known to be weakly-referenced. WeakReference garbage collected as soon as possible when VM needed memory

No comments:

Post a Comment

Feedback always help in improvement. If you have any query suggestion feel free to comment and Keep visiting my blog to encourage me to blogging

Android News and source code