The difference between weak and soft references in Java and the introduction of virtual and strong references

  • 2020-04-01 03:17:30
  • OfStack

Knowing the concepts of weak and soft references is not the same as knowing how to use them, and reference classes play an important role in garbage collection. We all know that the garbage collector recycles the memory of an object that meets the recycling criteria, but not all programmers know that the recycling criteria depend on the type of reference to the object. This is the main difference between weak and soft references in Java. If an object has only a weak reference to it, the garbage collector immediately recycles the object, which is an eager way to recycle. In contrast, if there is a soft reference to these objects, they are only recycled when memory is needed by the JVM. The special behavior of weak and soft references makes them useful in some situations. For example, soft references are good for caching, and when the JVM needs memory, the garbage collector will recycle objects that only the soft references point to. Weak references are good for storing metadata, for example: storing ClassLoader references. If no class is loaded, there is no reference to the ClassLoader. Once the last strong reference is removed, only the weak reference classloaders are recovered. In this article, we'll look at different types of Java references, such as Strong references and PhantomReference.

Weak versus soft references in Java

There are four types of references in Java:

1. Strong Reference
2. WeakReference
3. SoftReference
4. PhantomReference

A strong reference is the simplest reference we use in programming. For example, the variable s in the code String s= "ABC" is a strong reference to the String object "ABC". Any object pointed to by a strong reference cannot be collected by the garbage collector; it is needed in the program. A weak reference using Java. Lang. Ref. WeakReference class class, you can use the following code creates a weak reference:


Counter counter = new Counter(); // strong reference - line 1
WeakReference<Counter> weakCounter = new WeakReference<Counter>(counter); //weak reference
counter = null; // now Counter object is eligible for garbage collection

Now as long as you assign null value to the strong reference object counter, the object can be collected by the garbage collector. Because the object no longer has other strong references, even a weak reference to the object, weakCounter, cannot prevent the garbage collector from collecting the object. Conversely, if the object contains a soft reference, the Counter object is not immediately reclaimed unless memory is required by the JVM. Soft references in Java using Java. Lang. Ref. SoftReference class, you can use the following code to create soft references:


Counter prime = new Counter(); // prime holds a strong reference  �  line 2
SoftReference soft = new SoftReference(prime) ; //soft reference variable has SoftReference to Counter Object created at line 2

prime = null; // now Counter object is eligible for garbage collection but only be collected when JVM absolutely needs memory

After the strong reference is null, the second action of the code, object Counter, creates a soft reference that again does not prevent the garbage collector from collecting the object, but can delay the collection, as opposed to eagerly collecting the object in a weak reference. Given this distinction between soft and weak references, soft references are better suited to caching mechanisms, while weak references are better suited to storing metadata. Another example of using weak references is WeakHashMap, which is another implementation of the Map interface in addition to HashMap and TreeMap. WeakHashMap has a feature: the keys in the map are encapsulated as weak references, that is, once the strong reference is removed, the weak reference inside the WeakHashMap cannot prevent the object from being collected by the garbage collector.

Virtual reference is Java. Lang. Ref package in the package a third of the available references, using Java. Lang. Ref. The PhantomReference class. Objects with virtual references can be collected by the garbage collector at any time. Similar to weak and soft references, you can create virtual references with the following code:


DigitalCounter digit = new DigitalCounter(); // digit reference variable has strong reference  �  line 3
PhantomReference phantom = new PhantomReference(digit); // phantom reference to object created at line 3

digit = null;

Once the strong reference is removed, the DigitalCounter object in the third line can be collected by the garbage collector at any time. Because there is only one virtual reference to the object, the virtual reference cannot prevent the garbage collector from collecting the object.

In addition to understanding weak references, soft references, virtual references, and WeakHashMap, you need to understand ReferenceQueue. In the process of creating any weak, soft, or virtual references you can provide a ReferenceQueue called ReferenceQueue with the following code:


ReferenceQueue refQueue = new ReferenceQueue(); //reference will be stored in this queue for cleanup
DigitalCounter digit = new DigitalCounter();
PhantomReference<DigitalCounter> phantom = new PhantomReference<DigitalCounter>(digit, refQueue);

The reference instance is added to the reference queue, and you can retrieve the object at any time by querying the reference queue. The life cycle of an object can be described by the following figure:

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / img.jbzj.com/file_images/article/201404/201441785119596.gif? 201431785134 ">  
This is the difference between weak and soft references in Java. We also learned some basic reference classes: weak references, soft references, virtual references, and WeakHashMap and WeakHashMap. In summary, proper use of references can help the garbage collector better manage Java memory.


Related articles: