A brief introduction to strong references, soft references, weak references, and virtual references (the differences between virtual references and soft references and weak references, and the benefits of soft references). Whether we judge the number of references by reference counting method or whether the reference chain of an object is reachable by reachability analysis method, the survival of an object is related to “references”.
Prior to JDK1.2, the definition of a reference in Java was traditional: if the value of a data store of type Reference represented the starting address of another piece of memory, that piece of memory represented a reference.
After JDK1.2, Java has expanded the concept of reference, including strong reference, soft reference, weak reference, and virtual reference.
1. StrongReference
Most of the references we used before were actually strong references, which are the most commonly used references. If an object has a strong reference, it is like an essential household item, and the garbage collector will never collect it. When running out of memory, the Java virtual machine would rather throw outofMemoryErrors to abort the program than randomly recycle objects with strong references to resolve the memory problem.
2. SoftReference
If an object has only soft references, it is akin to an unnecessary commodity. If there is enough memory, the garbage collector will not reclaim it, and if there is not enough memory, it will reclaim the memory of these objects. As long as the garbage collector does not collect it, the object can be used by the program. Soft references can be used to implement memory sensitive caching.
A soft reference can be used in conjunction with a ReferenceQueue (ReferenceQueue), and if the object referenced by the soft reference is garbage collected, the JAVA virtual machine adds the soft reference to the ReferenceQueue associated with it.
3. WeakReference
If an object has only weak references, it is akin to an optional household item. The difference between weak and soft references is that objects with only weak references have a shorter lifetime. When the garbage collector thread scans the memory area under its control, once it finds an object with only weak references, it reclaims its memory regardless of whether the current memory space is sufficient. However, because the garbage collector is a low-priority thread, objects that have only weak references are not necessarily found quickly.
Weak references can be used in conjunction with a ReferenceQueue (ReferenceQueue), and if the object referenced by a weak reference is garbage collected, the Java virtual machine adds the weak reference to the ReferenceQueue associated with it.
4. Virtual Reference
A “virtual reference” is, as the name implies, a virtual reference. Unlike the other references, a virtual reference does not determine the lifetime of an object. If an object holds only virtual references, it can be garbage collected at any time, just as if there were no references at all.
Virtual references are mainly used to track the activity of objects being garbage collected.
One difference between a virtual reference and a soft or weak reference is that a virtual reference must be used in conjunction with a ReferenceQueue. When the garbage collector is about to reclaim an object and finds that it has a virtual reference, it adds the virtual reference to the reference queue associated with it before reclaiming the object’s memory. A program can determine whether a referenced object is about to be garbage collected by determining whether a virtual reference has been added to the reference queue. If a program finds that a virtual reference has been added to the reference queue, it can take the necessary action before the memory of the referenced object is reclaimed.
In particular, weak references and virtual references are rarely used in program design, but soft references are often used. This is because soft references can speed up the recycling of garbage memory by JVM, maintain the safety of system operation, and prevent memory overflow and other problems.
Receive 100 quality interview questions from Java for free