Difference Betwixt Weakreference Vs Softreference Vs Phantomreference Vs Potent Reference Inward Java
Thursday, January 3, 2019
Add Comment
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 role WeakReference in addition to SoftReference inward Java. Reference classes are especially of import inward 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 sort of references are pointing to that object. This is too master copy difference betwixt WeakReference in addition to SoftReference inward Java. Garbage collector tin mail away collect an object if exclusively weak references are pointing towards it in addition to they are eagerly collected, on the other mitt Objects amongst SoftReference are collected when JVM absolutely needs memory.
These special behavior of SoftReference and WeakReference makes them useful inward for sure cases e.g. SoftReference looks perfect for implementing caches, thence when JVM needs retentivity it removes object which convey exclusively SoftReference pointing towards them.
On the other mitt WeakReference is cracking for storing meta information e.g. storing ClassLoader reference. If no aeroplane is loaded in addition to thence no yell for inward keeping reference of ClassLoader, a WeakReference makes ClassLoader eligible for Garbage collection equally shortly equally final potent reference removed.
In this article nosotros volition explore to a greater extent than or less to a greater extent than close diverse reference inward Java e.g. Strong reference in addition to Phantom reference.
After making potent reference null, Counter object created on job two exclusively has i soft reference which tin mail away non foreclose it from existence garbage collected simply it tin mail away delay collection, which is eager inward illustration 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 illustration of WeakReference is WeakHashMap, which is to a greater extent than or less other implementation of Map interface similar HashMap or TreeMap but amongst i unique feature. WeakHashMap wraps keys equally WeakReference which agency i time potent reference to actual object removed, WeakReference present internally on WeakHashMap doesn't foreclose them from existence Garbage collected.
Phantom reference is 3rd sort of reference type available inward java.lang.ref package. Phantom reference is represented past times java.lang.ref.PhantomReference class. Object which exclusively has Phantom reference pointing them tin mail away hold upward collected whenever Garbage Collector likes it. Similar to WeakReference and SoftReference you tin mail away create PhantomReference by using next code :
As shortly equally y'all take Strong reference, DigitalCounter object created at job iii tin mail away hold upward garbage collected at whatever fourth dimension equally it exclusively has i to a greater extent than PhantomReference pointing towards it, which tin mail away non foreclose it from GC'd.
Apart from knowing close WeakReference, SoftReference, PhantomReference and WeakHashMap there is i to a greater extent than aeroplane called ReferenceQueue which is worth knowing. You tin mail away provide a ReferenceQueue instance piece creating whatever WeakReference, SoftReference or PhantomReference equally shown inward next code :
Reference of instance volition hold upward appended to ReferenceQueue and y'all tin mail away role it to perform whatever clean-up past times polling ReferenceQueue. An Object's life-cycle is nicely summed upward past times this diagram.
That's all on Difference betwixt WeakReference in addition to SoftReference inward Java. We too learned basics of reference classes e.g. Weak, soft in addition to phantom reference inward Java in addition to WeakHashMap and ReferenceQueue. Careful role of reference tin mail away assistance Garbage Collection in addition to lawsuit inward amend retentivity management inward Java.
Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!
Sumber https://javarevisited.blogspot.com/
These special behavior of SoftReference and WeakReference makes them useful inward for sure cases e.g. SoftReference looks perfect for implementing caches, thence when JVM needs retentivity it removes object which convey exclusively SoftReference pointing towards them.
On the other mitt WeakReference is cracking for storing meta information e.g. storing ClassLoader reference. If no aeroplane is loaded in addition to thence no yell for inward keeping reference of ClassLoader, a WeakReference makes ClassLoader eligible for Garbage collection equally shortly equally final potent reference removed.
In this article nosotros volition explore to a greater extent than or less to a greater extent than close diverse reference inward Java e.g. Strong reference in addition to Phantom reference.
WeakReference vs SoftReference inward Java
For those who don't know at that topographic point are 4 sort of reference inward Java :- Strong reference
- Weak Reference
- Soft Reference
- Phantom Reference
Now equally shortly equally y'all brand potent reference counter = null, counter object created on job 1 becomes eligible for garbage collection; because it doesn't convey whatever to a greater extent than Strong reference in addition to Weak reference past times reference variable weakCounter can non foreclose Counter object from existence garbage collected. On the other hand, had this been Soft Reference, Counter object is non garbage collected until JVM absolutely needs memory. Soft reference inward Java is represented using java.lang.ref.SoftReference class. You tin mail away role next code to create a SoftReference in JavaCounter counter = new Counter(); // potent reference - job 1 WeakReference<Counter> weakCounter = new WeakReference<Counter>(counter); //weak reference counter = null; // straightaway Counter object is eligible for garbage collection
Counter prime number = new Counter(); // prime number holds a potent reference - job 2 SoftReference<Counter> soft = new SoftReference<Counter>(prime) ; //soft reference variable has SoftReference to Counter Object created at job 2 prime number = null; // straightaway Counter object is eligible for garbage collection simply exclusively hold upward collected when JVM absolutely needs memory
Phantom reference is 3rd sort of reference type available inward java.lang.ref package. Phantom reference is represented past times java.lang.ref.PhantomReference class. Object which exclusively has Phantom reference pointing them tin mail away hold upward collected whenever Garbage Collector likes it. Similar to WeakReference and SoftReference you tin mail away create PhantomReference by using next code :
DigitalCounter digit = new DigitalCounter(); // digit reference variable has potent reference - job 3 PhantomReference<DigitalCounter> phantom = new PhantomReference<DigitalCounter>(digit); // phantom reference to object created at job 3 digit = null;
Apart from knowing close WeakReference, SoftReference, PhantomReference and WeakHashMap there is i to a greater extent than aeroplane called ReferenceQueue which is worth knowing. You tin mail away provide a ReferenceQueue instance piece creating whatever WeakReference, SoftReference or PhantomReference equally shown inward next code :
ReferenceQueue refQueue = new ReferenceQueue(); //reference volition hold upward stored inward this queue for cleanup DigitalCounter digit = new DigitalCounter(); PhantomReference<DigitalCounter> phantom = new PhantomReference<DigitalCounter>(digit, refQueue);
That's all on Difference betwixt WeakReference in addition to SoftReference inward Java. We too learned basics of reference classes e.g. Weak, soft in addition to phantom reference inward Java in addition to WeakHashMap and ReferenceQueue. Careful role of reference tin mail away assistance Garbage Collection in addition to lawsuit inward amend retentivity management inward Java.
Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!
0 Response to "Difference Betwixt Weakreference Vs Softreference Vs Phantomreference Vs Potent Reference Inward Java"
Post a Comment