The concepts of strong citation, soft citation, weak citation, and virtual citation
StrongReference
Strong quotations refer to common quotations in program code. For example, the object and str in the following code are both strong quotations:
Object object = new Object(); String str = "hello";
As long as an object has a strong reference associated with it, the JVM will definitely not recycle this object. Even in the absence of memory, the JVM would rather throw an OutOfMemory error than recycle such an object.
For example, the following code:
public class Main { public static void main(String[] args) { new Main().fun1(); } public void fun1() { Object object = new Object(); Object[] objArr = new Object[1000]; } }When running to Object[] objArr = new Object[1000]; when running to this sentence, if there is insufficient memory, the JVM will throw an OOM error and will not recycle the object pointed to by the object. However, it should be noted that after fun1 is run, both object and objArr no longer exist, so the objects they point to will be recycled by the JVM.
If you want to interrupt the association between a strong reference and an object, you can display the reference to null, so that the JVM will recycle the object at the appropriate time.
SoftReference
Soft references are used to describe some useful but not required objects, and are represented in Java using the java.lang.ref.SoftReference class. For soft references associated objects, the JVM will recycle the object only when there is insufficient memory. Therefore, this can be used well to solve the problem of OOM, and this feature is very suitable for implementing caching: such as web page caching, image caching, etc.
A soft reference can be used in conjunction with a reference queue (ReferenceQueue). If the object referenced by the soft reference is recycled by the JVM, the soft reference will be added to the associated reference queue. Here is an example of usage:
import java.lang.ref.WeakReference; public class Main { public static void main(String[] args) { WeakReference<String> sr = new WeakReference<String>(new String("hello")); System.out.println(sr.get()); System.gc(); //Notify the JVM's gc for garbage collection System.out.println(sr.get()); } }WeakReference
Weak references are also used to describe non-essential objects. When the JVM performs garbage collection, objects associated with weak references will be recycled regardless of whether the memory is sufficient. In java, use the java.lang.ref.WeakReference class to represent it. Here is an example of usage:
import java.lang.ref.WeakReference; public class Main { public static void main(String[] args) { WeakReference<String> sr = new WeakReference<String>(new String("hello")); System.out.println(sr.get()); System.gc(); //Notify the JVM's gc to perform garbage collection System.out.println(sr.get()); } }PhantomReference
Virtual references are different from the previous soft references and weak references, and they do not affect the life cycle of the object. In java, use java.lang.ref.PhantomReference class to represent it. If an object is associated with a virtual reference, it may be recycled by the garbage collector at any time, just like no reference is associated with it.
It should be noted that virtual references must be used in association with the reference queue. When the garbage collector is preparing to recycle an object, if it finds that it has a virtual reference, it will add this virtual reference to the associated reference queue. The program can understand whether the referenced object will be garbage collected by judging whether a virtual reference has been added to the reference queue. If the program finds that a virtual reference has been added to the reference queue, it can take necessary actions before the memory of the referenced object is recycled.
Further understanding of soft and weak citations
We often use strong citations when writing code. For the other three types of citations, the most commonly used are soft citations and weak citations. These two have both similarities and differences. They are all used to describe non-essential objects, but objects associated with soft references will only be recycled when there is insufficient memory, while objects associated with weak references will always be recycled when the JVM is garbage collected. In view of the above features, soft references are suitable for caching. When memory is insufficient, the JVM can recycle memory. Weak references can be used to prevent memory leakage in the callback function. Because callback functions are often anonymous internal classes and implicitly save references to external classes, if the callback function is called back in another thread, and if the external class needs to be recycled at this time, memory leaks, because the anonymous internal classes store strong references to external classes.
The above article briefly talks about the difference between the four citation methods in Java is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.