In versions prior to JDK1.2, when an object is not referenced by any variable, the program can no longer use this object. That is, a program can only use an object if it is reachable. This is just like in daily life, after buying an item from a store, if it is useful, you keep it, otherwise you throw it into the trash can and be collected by the cleaners. Generally speaking, if an item has been thrown into the trash can, it is impossible to pick it up and use it again.
But sometimes the situation is not that simple. You may encounter items that are tasteless and tasteless, and it is a pity to throw them away. This kind of item is no longer useful now. Keeping it will take up space, but it is not cost-effective to throw it away immediately because it may be used in the future. For such dispensable items, a compromise approach is: if there is enough space in the home, keep it at home first. If there is not enough space in the home, even if all the garbage is removed from the home, it still cannot accommodate the indispensable items. If you don’t have enough daily necessities, then throw away these dispensable items.
Starting from JDK version 1.2, object references are divided into four levels, allowing the program to more flexibly control the life cycle of the object. These four levels, from high to low, are: strong reference, soft reference, weak reference and virtual reference.
The quotations introduced earlier in this chapter are actually strong quotations, which are the most commonly used quotations. If an object has a strong reference, it is similar to an essential daily necessities, and the garbage collector will never reclaim it. When there is insufficient memory space, the Java virtual machine would rather throw an OutOfMemoryError error and cause the program to terminate abnormally, rather than arbitrarily recycling objects with strong references to solve the problem of insufficient memory.
If an object only has soft references, it is similar to disposable daily necessities. If there is enough memory space, the garbage collector will not reclaim it. If there is insufficient memory space, the memory of these objects will be reclaimed. 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.
3. Weak Reference
Weak references 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.
As the name suggests, "Phantom Reference" is in name only. Unlike other references, a phantom reference does not determine the life cycle of the object. If an object holds only phantom references, it is as if it had no references and may be garbage collected at any time.
Virtual references are mainly used to track the activities of objects being garbage collected. One difference between virtual references, soft references and weak references is that virtual references must be used in conjunction with a reference queue (ReferenceQueue). When the garbage collector is preparing to recycle an object, if it finds that it still has a virtual reference, it will add the virtual reference to the reference queue associated with it before recycling the object's memory. The program can determine whether the referenced object will be garbage collected by determining 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.
Three classes are provided in the java.lang.ref package: SoftReference class, WeakReference class and PhantomReference class, which represent soft references, weak references and phantom references respectively. The ReferenceQueue class represents a reference queue, which can be used in conjunction with these three reference classes to track the activities of the Java virtual machine in recycling the referenced objects. The following program creates a String object, ReferenceQueue object and WeakReference object:
//Create a strong reference
String str = new String("hello");
//Create a reference queue,
ReferenceQueue
//Create a weak reference, which references "hello" object, and is associated with the rq reference queue
//
WeakReference
The above is the content of strong, soft, weak and virtual references of Java objects. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!