Strong references: references to objects, such as new objects, are never recycled as long as strong references exist.
Strong references are the most commonly used references. If an object has a strong reference, the garbage collector will never collect it. As follows:
Object strongReference = new Object();
Copy the code
Soft references: Referenced but not required objects that are reclaimed before an overflow exception.
Soft references can be used in conjunction with a ReferenceQueue (ReferenceQueue). If the object referenced by the soft reference is garbage collected, the JAVA VIRTUAL machine adds the soft reference to the reference queue associated with it.
ReferenceQueue<String> referenceQueue = new ReferenceQueue<>();
String str = new String("abc");
SoftReference<String> softReference = new SoftReference<>(str, referenceQueue);
str = null;
// Notify GC
System.gc();
System.out.println(softReference.get()); // abc
Reference<? extends String> reference = referenceQueue.poll();
System.out.println(reference); //null
Copy the code
Weak references: non-essential objects that can survive until the next garbage collection occurs.
String str = new String("abc");
WeakReference<String> weakReference = new WeakReference<>(str);
str = null;
Copy the code
The life cycle of a WeakReference object is basically determined by the garbage collector. Once the garbage collector thread finds a WeakReference object, it will be collected in the next GC process.
Virtual reference: has no effect on lifetime and is notified at garbage collection time.
As the name implies, a virtual reference is a virtual reference. Unlike the other references, virtual references do not determine the lifetime of the object. If an object holds only virtual references, it can be collected by the garbage collector at any time, just as if there were no references at all.
Application Scenarios:
Virtual references are mainly used to track the activity of objects being collected by the garbage collector. A virtual reference differs from a soft or weak reference in that:
A virtual reference must be used in conjunction with a ReferenceQueue. When the garbage collector is about to reclaim an object and finds that it has a virtual reference, it adds the virtual reference to the reference queue associated with it before reclaiming the object’s memory.
String str = new String("abc");
ReferenceQueue queue = new ReferenceQueue();
// Create a virtual reference that must be associated with a reference queue
PhantomReference pr = new PhantomReference(str, queue);
Copy the code
A program can determine whether a referenced object is about to 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 the necessary action before the memory of the referenced object is reclaimed.
conclusion
In Java, the level and strength of the four references are in descending order: strong reference -> soft reference -> Weak reference -> virtual reference
When the garbage collector collects, some objects are collected and some are not. The garbage collector marks alive objects from the root Object, and then collects unreachable objects and referenced objects.
This article was published by YBG