1) Strong references (default supported mode)
When the JVM runs out of memory, it starts garbage collection. For strongly referenced objects, it does not collect them, even if they are in OOM. Strong references are our most common plain object references, and as long as there is a strong reference to an object, it indicates that the object is “alive” and won’t be touched by the garbage collector. The most common in Java is strong references, where an object is assigned to a reference variable that is a strong reference. When an object is referred to by a strongly referenced variable, it is in a reachable state and cannot be collected by the garbage collection mechanism, even if the object will never be used and the JVM will not reclaim it. So strong references are one of the main causes of Java memory leaks. An ordinary object that has no other references is generally considered ready for garbage collection as long as it exceeds the scope of the reference or explicitly sets the corresponding (strong) reference to NULL (although the timing of collection depends on the garbage collection policy).
2) Soft references
Soft references is a relatively strong reference to weaken some of reference, need to use Java. Lang. Ref. SoftReference class to implement, can let the object save some garbage collection. For objects that only have soft references: it is not reclaimed when the system is full of memory, but reclaimed when it is not. Soft references are usually used in memory-sensitive programs, such as caches, where memory is reserved and reclaimed when it is insufficient.
3) Weak references
Weak references need to use Java. Lang. Ref. WeakReference class, its survival period shorter than soft references. For weakly referenced objects, the garbage collection mechanism reclaims the object’s memory as soon as it runs, regardless of whether the JVM has enough memory.
4) Virtual references
You need to use Java. Lang. Ref. PhantomReference class to implement. As the name implies, virtual references do not determine the life cycle of an object, unlike other centralized references. If an object holds only virtual references, then it can be collected by the garbage collector at any time as if it had no references at all. It cannot be used alone or accessed through it. Virtual references must be used in conjunction with the ReferenceQueue (ReferenceQueue). The main purpose of a virtual reference is to track the status of an object being garbage collected. It just provides a mechanism to make sure that objects do something when they are finalize. PhantomReference’s get method always returns NULL, so the corresponding reference object cannot be accessed. The meaning is to indicate that an object has entered the finalization stage and can be collected by the GC for more flexible collection operations than finalization mechanisms. In other words, the only purpose of setting up a virtual reference association is to receive a system notification or add further processing later when the object is reclaimed by the collector. Java technology allows you to use the Finalize () method to do the necessary cleanup before the garbage collector purifies objects from memory.