Monday 19 February 2018

Strong, Soft, Weak and Phantom References in Java

(Download One Minute Java Mobile app for latest updates)

Strong References
Strong references are the normal references which you create by new keyword
These references are not cleared until the reference is marked as null or the scope in which reference is created is executed completely

Soft References
These are the references which are marked for garbage collection, but garbage collector may not clear them till there is sufficient memory available
Garbage collector ensures that all soft references are cleared before throwing OutOfMemoryError.
This mean, soft references may remain in memory until JVM has exhausted all its heap memory and when there is a dire need of memory it will clear all soft references

Example

  Employee emp = new Employee();

 //Create Soft reference for emp object
 SoftReference&ltEmployee&gt softRefForEmp = new SoftReference&ltEmployee&gt(emp);

 //mark emp object for Garbage Collection
 emp = null;

 //if the soft reference is not yet cleared by garbage collector you can still get back the object in following way

 emp = softRefForEmp.get();

 //if the soft reference is cleared by garbage collector the above method will return null

Usage of Soft References
Direct instances of this class may be used to implement simple caches; this class or derived subclasses may also be used in larger data structures to implement more sophisticated caches. As long as the referent of a soft reference is strongly reachable, that is, is actually in use, the soft reference will not be cleared. Thus a sophisticated cache can, for example, prevent its most recently used entries from being discarded by keeping strong referents to those entries, leaving the remaining entries to be discarded at the discretion of the garbage collector.

Weak References
These references are garbage collected when there is no strong or soft references to the referent
Weak References are garbage collected even though plenty of memory is available
Example of weak reference could be exactly similar as above so I am not repeating it here. Only change is you have to use WeakReference class

Usage of Weak References
Weak references are most often used to implement canonicalizing mappings.
Canonical mappings are used in the scenarios where our dataset is huge and we have only limited memory, in such cases we can use weak references so that GC can remove used data immediately to load fresh set of data

Phantom References
When there is no strong, soft and weak references to the referent Phantom references can be garbage collected
Phantom references are the objects which are finalized but not yet cleared by garbage collector.
Memory management can be a issue while using phantom references this is because as per java docs:

  • Unlike soft and weak references, phantom references are not automatically cleared by the garbage collector as they are enqueued. An object that is reachable via phantom references will remain so until all such references are cleared or themselves become unreachable

As we have seen in the above example object of the referent can be retrived in cases of SoftReferences and WeakReferences but in case of phantom references get() method always return null

Usage of Phantom References
Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism.

No comments:

Post a Comment