Virtual machine-----strong reference, soft reference, weak reference, phantom reference

Whether it is to judge the number of object references through the reference counting method, or to judge whether the reference chain of the object is reachable through the reachability analysis method, judging the survival of the object is related to the "reference".

Before JDK1.2, the definition of reference in Java was very traditional: if the value stored in the reference type data represents the starting address of another block of memory, it is said that the reference data represents a reference to a certain object within a certain block.

After JDK1.2, Java has expanded the concept of references, dividing references into four types: strong references, soft references, weak references, and phantom references (the intensity of references is gradually weakening)

Strongly Reference

In the past, most of our references were actually strong references, referring to the common reference assignments in the program code, that is, similar Object obj = new Object(), this kind of reference relationship. If an object has a strong reference, it is similar to an essential daily necessities , and the garbage collector will never recycle it. When the memory space is insufficient, the Java virtual machine would rather throw OutOfMemoryErroran error to cause the program to terminate abnormally, and would not solve the memory shortage problem by reclaiming objects with strong references at will.

Soft Reference

If an object only has soft references, it is similar to a dispensable daily necessities . If the memory space is sufficient, the garbage collector will not reclaim it, and if the memory space is insufficient, the memory of these objects will be reclaimed. As long as the garbage collector does not reclaim it, the object can be used by the program. Soft references can be used to implement memory-sensitive caches.

The soft reference can be used in conjunction with a reference queue (ReferenceQueue). If the object referenced by the soft reference is garbage collected, the Java virtual machine will add the soft reference to the reference queue associated with it.

After JDK1.2 version, the SoftReference class is provided to create soft references.

Object obj = new Object();
SoftReference<Object> sf = new SoftReference<Object>(obj);
obj = null;  // 使对象只被软引用关联

Weak Reference

If an object has only weak references, it is similar to a dispensable daily necessities . The difference between weak references and soft references is that only objects with weak references have a shorter life cycle. In the process of the garbage collector thread scanning the memory area under its jurisdiction, once an object with only weak references is found, its memory will be reclaimed regardless of whether the current memory space is sufficient. **However, because the garbage collector is a low-priority thread, objects that only have weak references may not be discovered soon.

A weak reference can be used in conjunction with a reference queue (ReferenceQueue). If the object referenced by the weak reference is garbage collected, the Java virtual machine will add the weak reference to the reference queue associated with it.

After the JDK1.2 version, the WeakReference class is provided to create weak references.

Object obj = new Object();
WeakReference<Object> wf = new WeakReference<Object>(obj);
obj = null;

Phantom Reference

Also known as ghost reference or phantom reference, whether an object has a phantom reference will not affect its survival time, and an object cannot be obtained through a phantom reference.

The only purpose of setting a phantom reference for an object is to receive a system notification when the object is recycled.

Phantom references are mainly used to track the activities of objects being garbage collected .

The PhantomReference class is provided after JDK1.2 to create weak references.

Object obj = new Object();
PhantomReference<Object> pf = new PhantomReference<Object>(obj, null);
obj = null;

One difference between phantom reference and soft reference and weak reference is that phantom reference must be used in conjunction with reference queue (ReferenceQueue). When the garbage collector is about to recycle an object, if it finds that it still has a phantom reference, it will add the phantom reference to the reference queue associated with it before reclaiming the object's memory. The program can determine 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 phantom reference has been added to the reference queue, it can take necessary actions before the memory of the referenced object is reclaimed.

Pay special attention to the fact that weak references and phantom references are rarely used in program design, and soft references are used in many cases. This is because soft references can speed up the recycling of garbage memory by the JVM, maintain the safety of the system, and prevent memory overflow. (OutOfMemory) and other problems .

Guess you like

Origin blog.csdn.net/hello_cmy/article/details/105874932