JavaRush /Java Blog /Random EN /Differences between weak, soft, phantom and regular refer...
theGrass
Level 24
Саратов

Differences between weak, soft, phantom and regular references in Java

Published in the Random EN group
“Weak” references and “soft” references (WeakReference, SoftReference) were added to the Java API a long time ago, but not every programmer is familiar with them. This indicates a gap in understanding where and how to use them. Reference classes are especially important in the context of garbage collection . As we all know, the garbage collector itself frees the memory occupied by objects, but not all programmers know that it makes the decision to free memory based on the type of references available to the object. Differences between weak, soft, phantom and regular links in Java - 1The main difference between SoftReference and WeakReference is how the collector will work with them. It can delete an object at any time if only weak links point to it, on the other hand, objects with a soft link will be collected only when the JVM really needs memory. Due to these features of reference classes, each of them has its own use. SoftReference can be used to implement caches and when the JVM needs memory it will free it by deleting such objects. And WeakReferences are great for storing metadata, for example, storing a link to a ClassLoader. If there are no classes to load then there is no point in storing a reference to the ClassLoader , a weak reference makes the ClassLoader available for removal as soon as we assign it instead of a Strong reference. In this article we will look at the differences between link types, including Strong reference and Phantom reference .

WeakReference vs SoftReference in Java

For those who don’t know, there are 4 types of links:
  1. Strong reference
  2. Weak Reference
  3. Soft reference
  4. Phantom Reference
The strong link is the simplest, since we use it in programming every day, for example, in code like String s = “abc” the variable s is the strong link. Any object that has a strong reference is prohibited from being removed by the garbage collector. Of course, these are objects that the Java program needs. Weak references are represented by the java.lang.ref.WeakReference class , you can define a weak reference like this:
Counter counter = new Counter(); // strong reference
WeakReference weakCounter = new WeakReference(counter); //weak reference
counter = null; // now Counter object is eligible for garbage collection
Now, once you set the strong reference counter to null (counter = null), the object created in the first line becomes available for garbage collection collection because it no longer has a strong reference. The weakCounter reference created by Weak cannot prevent the collector from deleting the Counter object. On the other hand, if it were a Soft reference, the Counter object would not be deleted until the JVM had a particularly high need for memory. Soft references in Java are represented by the java.lang.ref.SoftReference class . Example of creating a SoftReference in Java
Counter prime = new Counter();  // prime holds a strong reference
SoftReference soft = new SoftReference(prime) ; //soft reference variable has SoftReference to Counter Object
prime = null;  // now Counter object is eligible for garbage collection but only be collected when JVM absolutely needs memory
After clearing the strong reference (in the 3rd line), only 1 soft reference will remain to the Counter object, which will not be able to prevent the garbage collector from deleting this object, but unlike a weak reference, it will be able to postpone this process until there is an acute shortage of memory. Given this difference between soft links and weak links, the first is more suitable for caches, and weak for metadata. A good example is the WeakHashMap class, which is a descendant of the Map interface like the HashMap or TreeMap classes , but with one distinctive feature. WeakHashMap wraps keys as weak references, which means that as soon as there are no strong references to an object, weak references that are located inside the WeakHashMap will not save you from the garbage collector. Phantom links are the third type of links available in the java.lang.ref package. Phantom references are represented by the java.lang.ref.PhantomReference class . An object pointed to only by phantom links can be deleted by the collector at any time. A Phantom link is created in the same way as a weak or soft link.
DigitalCounter digit = new DigitalCounter(); // digit reference variable has strong reference
PhantomReference phantom = new PhantomReference(digit); // phantom reference
digit = null;
Once you clear the strong references to the DigitalCounter object, the garbage collector will delete it at any time, since now only phantom references point to it. In addition to the WeakReference, SoftReference, PhantomReference, WeakHashMap classes, it is useful to know about the ReferenceQueue class . You can use this class when creating a WeakReference, SoftReference, or PhantomReference object:
ReferenceQueue refQueue = new ReferenceQueue(); //reference will be stored in this queue for cleanup
DigitalCounter digit = new DigitalCounter();
PhantomReference phantom = new PhantomReference(digit, refQueue);
The object reference will be added to the ReferenceQueue and you can monitor the state of the references by polling the ReferenceQueue. The life cycle of Object is well represented in this diagram: Differences between weak, soft, phantom and regular references in Java - 2That's all the differences between weak and soft references in Java . We also got acquainted with phantom links, the WeakHashMap class and ReferenceQueue . Using references correctly will help with garbage collection and will result in more flexible memory management in Java . Original article
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION