Difference Betwixt Weakreference Vs Softreference Vs Phantomreference Vs Rigid Reference Inward Java

Difference Betwixt Weakreference Vs Softreference Vs Phantomreference Vs Rigid Reference Inward Java - Hallo sahabat BEST LEARNING JAVA, Pada Artikel yang anda baca kali ini dengan judul Difference Betwixt Weakreference Vs Softreference Vs Phantomreference Vs Rigid Reference Inward Java, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel core java, Artikel interview questions, Artikel programming, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : Difference Betwixt Weakreference Vs Softreference Vs Phantomreference Vs Rigid Reference Inward Java
link : Difference Betwixt Weakreference Vs Softreference Vs Phantomreference Vs Rigid Reference Inward Java

Baca juga


Difference Betwixt Weakreference Vs Softreference Vs Phantomreference Vs Rigid Reference Inward Java

WeakReference and SoftReference were added into Java API from long fourth dimension only non every Java programmer is familiar amongst it. Which agency at that spot is a gap betwixt where together with how to role WeakReference together with SoftReference inwards Java. Reference classes are peculiarly 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, only non many programmer knows that this eligibility is decided based upon which form of references are pointing to that object. This is too primary difference betwixt WeakReference together with SoftReference inwards Java. Garbage collector tin collect an object if alone weak references are pointing towards it together with 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 sure enough cases e.g. SoftReference looks perfect for implementing caches, thus when JVM needs retentivity it removes object which convey alone SoftReference pointing towards them.

On the other manus WeakReference is groovy for storing meta information e.g. storing ClassLoader reference. If no bird is loaded thus no betoken inwards keeping reference of ClassLoader, a WeakReference makes ClassLoader eligible for Garbage collection every bit presently every bit final potent reference removed.

In this article nosotros volition explore approximately to a greater extent than close diverse reference inwards Java e.g. Strong reference together with Phantom reference.



WeakReference vs SoftReference inwards Java

For those who don't know at that spot are 4 form of reference inwards Java :
  1. Strong reference
  2. Weak Reference
  3. Soft Reference
  4. Phantom Reference
Strong Reference is most uncomplicated every bit nosotros role it inwards our solar daytime to solar daytime programming life e.g. inwards the code, String second = "abc" , reference variable s has potent 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 past times Java program. Weak Reference are represented using java.lang.ref.WeakReference bird together with you lot tin create Weak Reference past times using next code :

Counter counter = new Counter(); // potent reference - job 1 WeakReference<Counter> weakCounter = new WeakReference<Counter>(counter); //weak reference counter = null; // at ane time Counter object is eligible for garbage collection
Now every bit presently every bit you lot brand potent reference counter = null, counter object created on job 1 becomes eligible for garbage collection; because it doesn't convey whatsoever to a greater extent than Strong reference together with Weak reference past times reference variable weakCounter can non forestall 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 role next code to create a SoftReference in Java

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;  // at ane time Counter object is eligible for garbage collection only alone last collected when JVM absolutely needs memory
After making potent reference null, Counter object created on job ii alone has ane soft reference which tin non forestall it from beingness garbage collected only it tin delay collection, which is eager inwards illustration of WeakReference. Due to this major difference betwixt SoftReference together with 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 approximately other implementation of Map interface similar HashMap or TreeMap but amongst ane unique feature. WeakHashMap wraps keys every bit WeakReference which agency ane time potent reference to actual object removed, WeakReference present internally on WeakHashMap doesn't forestall them from beingness Garbage collected.

Phantom reference is 3rd form of reference type available inwards java.lang.ref package. Phantom reference is represented past times java.lang.ref.PhantomReference class. Object which alone has Phantom reference pointing them tin last 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 potent reference - job 3 PhantomReference<DigitalCounter> phantom = new PhantomReference<DigitalCounter>(digit); // phantom reference to object created at job 3  digit = null;
As presently every bit you lot take away Strong reference, DigitalCounter object created at job iii tin last garbage collected at whatsoever fourth dimension every bit it alone has ane to a greater extent than PhantomReference pointing towards it, which tin non forestall it from GC'd.

Apart from knowing close WeakReference, SoftReference, PhantomReference and WeakHashMap there is ane to a greater extent than bird called ReferenceQueue which is worth knowing. You tin provide a ReferenceQueue instance land creating whatsoever WeakReference, SoftReference or PhantomReference every bit shown inwards next code :

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

into Java API from long fourth dimension only 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 together with SoftReference inwards Java. We too learned basics of reference classes e.g. Weak, soft together with phantom reference inwards Java together with WeakHashMap and ReferenceQueue. Careful role of reference tin attention Garbage Collection together with lawsuit inwards improve retentivity management inwards Java.

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




Demikianlah Artikel Difference Betwixt Weakreference Vs Softreference Vs Phantomreference Vs Rigid Reference Inward Java

Sekianlah artikel Difference Betwixt Weakreference Vs Softreference Vs Phantomreference Vs Rigid Reference Inward Java kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel Difference Betwixt Weakreference Vs Softreference Vs Phantomreference Vs Rigid Reference Inward Java dengan alamat link https://bestlearningjava.blogspot.com/2020/03/difference-betwixt-weakreference-vs.html

Belum ada Komentar untuk "Difference Betwixt Weakreference Vs Softreference Vs Phantomreference Vs Rigid Reference Inward Java"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel