After JDK1.2, Java expanded the concept of references into strong references, soft references, weak references, and virtual references (strong weak virtual). The intensity of these four citations declines in turn. With the exception of strong references, the other three references are available under the java.lang.ref package.

The top-level interface is Reference, which also has a finalizer implementation class, but this class is visible in the package and is rarely used.

StrongReference: a traditional and basic reference, which is a reference assignment commonly found in your code. Object obj = new Object(). In any case, the garbage collector will never reclaim the referenced object as long as the strongly referenced relationship exists.

SoftReference (SoftReference) : before the memory is about to be exhausted, a SoftReference object is included in the collection scope for recycling. If there is not enough memory space, the OOM will be thrown.

WeakReference: objects associated with weak references can only survive until the next garbage collection. When the garbage collector works, the objects associated with weak references will be reclaimed regardless of whether the memory space is sufficient.

Virtual reference: The existence of a virtual reference does not affect the lifetime of an object and cannot be used to obtain an instance of an object. The only purpose of setting up a virtual reference association for an object is to receive a system notification when the object is reclaimed.

Strong references – OOM is not recycled either

In Java programs, the most common reference relationship is strong reference, which is the default reference type.

Strongly referenced objects are reachable, and the garbage collector will never reclaim referenced objects. An ordinary object can be garbage collected if it has no other reference relationship, exceeds the scope of the reference, and explicitly assigns the reference relationship to NULL.

Soft references, weak references, and virtual references correspond to soft, weak, and virtual touchables respectively. Under certain conditions, they can all be reclaimed. Objects with strong references cannot be reclaimed, so strong references are one of the main causes of memory leaks.

// Strong reference assignment
String str = new Sting("hello lwx");
Copy the code

Soft reference – When memory runs out, it is reclaimed

Some useful but not necessary objects can be described by soft references. For example, if memory is used to implement memory sensitive cache, if there is free memory, the cache can be temporarily reserved, when the memory is insufficient to clean up soft reference objects, to ensure that memory will not run out.

At some point, when the garbage collector decides to reclaim soft reachable objects, it automatically places the corresponding references in a reference queue. Through this queue, you can track the collection of objects.

The JDK provides SoftReference to implement soft references

// Wrap a string object as a soft reference
String str = new String("hell lwx");
SoftReference<String> sf = new SoftReference(str);

// Destroy strong references
str = null;

// Get the soft-referenced object
String str1 = sf.get();
Copy the code

Weak references – Discover and recycle

Weak references can also be used to describe non-essential objects, objects associated with weak references that can only survive until the next garbage collection occurs. During GC, objects associated with weak references are reclaimed regardless of whether there is sufficient memory.

However, because the thread priority of the garbage collector is usually low, it is not always easy to find weakly referenced objects. In this case, weakly referenced objects can survive for a long time.

Weak references are more easily collected by GC than soft references.

The corresponding packaging class of WeakReference is WeakReference. WeakHashMap uses a lot of weak references internally.

Virtual reference – Object reclamation trace

PhantomReference is also called PhantomReference, and phantom application is the weakest of all reference types.

Virtual references cannot be used alone either; when an object is attempted to be retrieved by get() of a virtual reference, it is always null. An object wrapped as a virtual reference whose sole purpose is to track the garbage collection process through the virtual reference.

When a vreferential object is created, it must also provide a reference queue as a parameter. When the GC is ready to reclaim an object, if it finds that it has a virtual reference, it will add the virtual reference to the reference queue after the object is reclaimed, so that the application object can be reclaimed. Therefore, some resource release operations can be performed and logged in virtual references.

// Wrap the virtual reference object
ReferenceQueue queue = new Reference();
PhantomReference<String> pr = new PhantomReference<>(new String("hello lwx"),queue);

// Do garbage collection
System.gc();

// Retrieves the reclaimed virtual reference from the queue
String str = queue.remove();
if(str ! =null){
    System.out.println("Vreference object is reclaimed");
}
Copy the code

Finalizer reference

Finalizer Reference is also the implementation class of Reference, which is used to implement Finalize () of objects without manual encoding. It is used internally with Reference queue.

Finalizer thread finds the referenced object through the Finalizer reference and calls Finalize (). The referenced object will be collected in the second GC.