There are four types of references in Java development, namely:
-
Strong references (strongreference) are the most commonly used references. If an object has a strong reference,
The garbage collector will never recycle it. When there is not enough memory space, the Java virtual Machine prefers to throw a outofmemoryerror error, which causes the program to terminate abnormally, and does not rely on random recycling of strongly referenced objects to resolve out-of-memory issues.
-
Soft Reference (SoftReference) If an object has only soft references, the memory space is sufficient,
The garbage collector does not recycle it, and if the memory space is insufficient, the memory of those objects is reclaimed.
The object can be used by the program as long as it is not reclaimed by the garbage collector. Soft references can be used to implement memory-sensitive caches.
A soft reference can be used in conjunction with a reference queue (Referencequeue), if the object referenced by the soft reference is reclaimed by the garbage collector,
The Java Virtual machine will add this soft reference to the reference queue associated with it.
-
Weak reference (WeakReference) weak references differ from soft references in that objects with only weak references have a shorter life cycle.
When the garbage collector thread scans the area of memory it governs, once an object with only weak references is found,
It will reclaim its memory regardless of the current memory space or not. However, because the garbage collector is a very low-priority thread,
It is not always quick to find objects that have only weak references. 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 adds the weak reference to the reference queue associated with it.
-
Virtual Reference (phantomreference) "virtual reference" as the name implies, is the form of a dummy,
Unlike several other references, a virtual reference does not determine the life cycle of an object.
If an object holds only a virtual reference, it is the same as without any reference.
May be reclaimed by the garbage collector at any time.
Virtual references are primarily used to track activities that objects are reclaimed by the garbage collector.
One difference between a virtual reference and a soft reference and a weak reference is that the virtual reference must be used in conjunction with the reference queue (Referencequeue). When the garbage collector prepares to reclaim an object, if it finds that it has a virtual reference, it will add the virtual reference to the reference queue associated with it before reclaiming the object's memory.
总结:WeakReference与SoftReference都可以用来保存对象的实例引用,这两个类与垃圾回收有关。
SoftReference is a strong reference to the object instance that it holds, and will not be reclaimed by GC unless the JVM is about to OutOfMemory.
This feature makes it particularly suitable for the design object cache. For the cache, we want the cached object to always be memory-resident,
But if the JVM's memory is tight, the system crashes in order not to occur outofmemoryerror
It also allows the JVM to reclaim the cache's memory when necessary, and then reload the data into the cache for the next appropriate time.
This allows the system to be designed more flexibly.
Four types of references exist in Java