Difference Betwixt Weakreference Vs Softreference Vs Phantomreference Vs Potent Reference Inwards Java

WeakReference and SoftReference were added into Java API from long fourth dimension simply non every Java programmer is familiar amongst it. Which agency at that topographic point is a gap betwixt where in addition to how to piece of occupation WeakReference in addition to SoftReference inwards Java. Reference classes are specially of import inwards context of How Garbage collection works. As nosotros all know that Garbage Collector reclaims retentivity from objects which are eligible for garbage collection, simply non many programmer knows that this eligibility is decided based upon which form of references are pointing to that object. This is equally good primary difference betwixt WeakReference in addition to SoftReference inwards Java. Garbage collector tin collect an object if entirely weak references are pointing towards it in addition to they are eagerly collected, on the other manus Objects amongst SoftReference are collected when JVM absolutely needs memory.

These special demeanour of SoftReference and WeakReference makes them useful inwards certainly cases e.g. SoftReference looks perfect for implementing caches, thence when JVM needs retentivity it removes object which convey entirely SoftReference pointing towards them.

On the other manus WeakReference is neat for storing meta information e.g. storing ClassLoader reference. If no shape is loaded in addition to then no betoken inwards keeping reference of ClassLoader, a WeakReference makes ClassLoader eligible for Garbage collection equally presently equally concluding rigid reference removed.

In this article nosotros volition explore about to a greater extent than most diverse reference inwards Java e.g. Strong reference in addition to Phantom reference.



WeakReference vs SoftReference inwards Java

For those who don't know at that topographic point are 4 form of reference inwards Java :
  1. Strong reference
  2. Weak Reference
  3. Soft Reference
  4. Phantom Reference
Strong Reference is most uncomplicated equally nosotros piece of occupation it inwards our twenty-four hours to twenty-four hours programming life e.g. inwards the code, String second = "abc" , reference variable s has rigid reference to String object "abc". Any object which has Strong reference attached to it is not eligible for garbage collection. Obviously these are objects which is needed yesteryear Java program. Weak Reference are represented using java.lang.ref.WeakReference shape in addition to you lot tin create Weak Reference yesteryear using next code :

Counter counter = new Counter(); // rigid reference - business 1 WeakReference<Counter> weakCounter = new WeakReference<Counter>(counter); //weak reference counter = null; // right away Counter object is eligible for garbage collection
Now equally presently equally you lot brand rigid reference counter = null, counter object created on business 1 becomes eligible for garbage collection; because it doesn't convey whatever to a greater extent than Strong reference in addition to Weak reference yesteryear reference variable weakCounter can non foreclose Counter object from beingness garbage collected.  On the other hand, had this been Soft Reference, Counter object is non garbage collected until JVM absolutely needs memory. Soft reference inwards Java is represented using java.lang.ref.SoftReference class. You tin piece of occupation next code to create a SoftReference in Java

Counter prime number = new Counter();  // prime number holds a rigid reference - business 2 SoftReference<Counter> soft = new SoftReference<Counter>(prime) ; //soft reference variable has SoftReference to Counter Object created at business 2  prime number = null;  // right away Counter object is eligible for garbage collection simply entirely endure collected when JVM absolutely needs memory
After making rigid reference null, Counter object created on business two entirely has 1 soft reference which tin non foreclose it from beingness garbage collected simply it tin delay collection, which is eager inwards instance of WeakReference. Due to this major difference betwixt SoftReference in addition to WeakReference, SoftReference are to a greater extent than suitable for caches and WeakReference are to a greater extent than suitable for storing meta data. One convenient instance of WeakReference is WeakHashMap, which is about other implementation of Map interface similar HashMap or TreeMap but amongst 1 unique feature. WeakHashMap wraps keys equally WeakReference which agency 1 time rigid reference to actual object removed, WeakReference present internally on WeakHashMap doesn't foreclose them from beingness Garbage collected.

Phantom reference is 3rd form of reference type available inwards java.lang.ref package. Phantom reference is represented yesteryear java.lang.ref.PhantomReference class. Object which entirely has Phantom reference pointing them tin endure collected whenever Garbage Collector likes it. Similar to WeakReference and SoftReference you tin create PhantomReference by using next code :

DigitalCounter digit = new DigitalCounter(); // digit reference variable has rigid reference - business 3 PhantomReference<DigitalCounter> phantom = new PhantomReference<DigitalCounter>(digit); // phantom reference to object created at business 3  digit = null;
As presently equally you lot take away Strong reference, DigitalCounter object created at business three tin endure garbage collected at whatever fourth dimension equally it entirely has 1 to a greater extent than PhantomReference pointing towards it, which tin non foreclose it from GC'd.

Apart from knowing most WeakReference, SoftReference, PhantomReference and WeakHashMap there is 1 to a greater extent than shape called ReferenceQueue which is worth knowing. You tin render a ReferenceQueue instance acre creating whatever WeakReference, SoftReference or PhantomReference equally shown inwards next code :

ReferenceQueue refQueue = new ReferenceQueue(); //reference volition endure stored inwards this queue for cleanup  DigitalCounter digit = new DigitalCounter(); PhantomReference<DigitalCounter> phantom = new PhantomReference<DigitalCounter>(digit, refQueue);
Reference of instance volition endure appended to ReferenceQueue and you lot tin piece of occupation it to perform whatever clean-up yesteryear polling ReferenceQueue. An Object's life-cycle is nicely summed upwards yesteryear this diagram.

into Java API from long fourth dimension simply non every Java programmer is familiar amongst it Difference betwixt WeakReference vs SoftReference vs PhantomReference vs Strong reference inwards Java


That's all on Difference betwixt WeakReference in addition to SoftReference inwards Java. We equally good learned basics of reference classes e.g. Weak, soft in addition to phantom reference inwards Java in addition to WeakHashMap and ReferenceQueue. Careful piece of occupation of reference tin attention Garbage Collection in addition to resultant inwards amend retentivity administration inwards Java.

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!


Komentar

Postingan populer dari blog ini

Difference Betwixt Struts Validatorform Vs Validatoractionform - Interview Question

How To Convert Inputstream To Byte Array Inwards Coffee - Two Examples

Difference Betwixt Fileinputstream Together With Filereader Inwards Coffee | Inputstream Vs Reader