Knowing the concepts of weak and soft references is different from how to use them. Reference classes play an important role in the process of garbage collection. We all know that the garbage collector reclaims memory for objects that meet the recycling criteria, but not all programmers know that the recycling criteria depends on the type of reference pointing to the object. This is the main difference between weak references and soft references in Java. If an object has only a weak reference pointing to it, the garbage collector recycles the object immediately, which is an urgent way to recycle it. Relatively, if there are soft references pointing to these objects, they will only be recycled if the JVM requires memory. The special behavior of weak and soft citations makes them very useful in some cases. For example: Soft references can be used well to implement cache. When the JVM needs memory, the garbage collector will recycle these objects that are only pointed to by soft references. Weak references are very suitable for storing metadata, such as storing ClassLoader references. If no class is loaded, there is no reference to ClassLoader either. Once the last strong reference is removed, only the weak reference ClassLoader will be recycled. In this article, we will talk about different types of Java references, such as: Strong Reference and PhantomReference.
Weak references vs. soft references in Java
There are four types of references in Java:
1. Strong Reference
2. WeakReference
3. SoftReference
4. PhantomReference
Strong references are the simplest references we use in programming, such as 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 recycled by the garbage collector, and these objects are needed in the program. Weak references are represented by the java.lang.ref.WeakReference class class class, and you can create weak references using the following code:
The code copy is as follows:
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 counter of the strong reference object, the object can be recycled by the garbage collector. Because the object no longer contains other strong references at this time, even weakCounter points to the object cannot prevent the garbage collector from recycling the object. Conversely, if the object contains a soft reference, the Counter object is not recycled immediately unless the JVM requires memory. Soft references in Java are represented by the java.lang.ref.SoftReference class. You can create soft references using the following code:
The code copy is as follows:
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 empty, the second behavior of the code, Counter, creates a soft reference. This reference also cannot prevent the garbage collector from recycling objects, but can delay recycling, which is different from urgent recycling objects in weak references. Given this difference between soft references and weak references, soft references are more suitable for caching mechanisms, while weak references are more suitable for storing metadata. Another example of using weak references is WeakHashMap, which is another implementation of the Map interface besides HashMap and TreeMap. WeakHashMap has a feature: the keys in the map are encapsulated as weak references, which means that once the strong reference is deleted, the weak references inside the WeakHashMap cannot prevent the object from being collected by the garbage collector.
Virtual reference is the third available reference in the java.lang.ref package, represented by the java.lang.ref.PhantomReference class. Objects with virtual references can be recycled by the garbage collector at any time. Similar to weak references and soft references, you can create virtual references through the following code:
The code copy is as follows:
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 row can be recycled by the garbage collector at any time. Because there is only one virtual reference pointing to the object, the virtual reference cannot prevent the garbage collector from reclaiming the object.
In addition to understanding weak references, soft references, virtual references, and WeakHashMap, you also need to understand ReferenceQueue. In the process of creating any weak references, soft references, and virtual references, you can provide the reference queue ReferenceQueue through the following code:
The code copy is as follows:
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);
Reference instances are added to the reference queue, and you can recycle objects by querying the reference queue any time. The life cycle of an object can be described in the figure below: