There are four reference types in the JVM
Strong reference
- As long as there is a strong reference, the garbage collector will never collect for example: Object obj = new Object();
- Help garbage object collection obj = NULL, ArrayList source code implementation
/**
* Removes all of the elements from this list. The list will
* be empty after this call returns.
*/
public void clear() {
modCount++;
// clear to let GC do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
Copy the code
Soft references
-
A term used to describe objects that are useful but not necessary. For objects associated with soft applications, before the system is about to run out of memory (Full GC), the objects will be listed in the recycle scope for a second collection. If there is not enough memory for this collection, an OOM exception will be raised.
-
SoftReference class is provided after JDK1.2 to implement soft references. This feature is ideal for web page caching, image caching, and so on.
- Example of browser web caching
Prev = new Browser(); // enter a SoftReference SoftReference sr = new SoftReference(). if (sr.get() == null) { prev = rs.get(); } else { prev = new Browser(); sr = new SoftReference(prev); }Copy the code
- A soft reference can be used in conjunction with a ReferenceQueue (ReferenceQueue), and if the soft reference object is collected by the garbage collector, the virtual machine adds the soft reference to the ReferenceQueue associated with it.
A weak reference
- It differs from soft references in that objects that only have weak references have a shorter lifetime. When the garbage collector thread scans the memory area under its control, once it finds an object with only weak references, it reclaims its memory regardless of whether the current memory space is sufficient.
- After JDK1.2, WeakReference classes are provided to implement weak references
- Example:
import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; import java.lang.ref.WeakReference; import java.util.LinkedList; public class ReferenceTest { private static ReferenceQueue<VeryBig> rq = new ReferenceQueue<>(); public static void checkQueue() { Reference<? extends VeryBig> ref = null; while ((ref = rq.poll()) ! = null) { if (ref ! = null) { System.out.println("In queue:" + ((VeryBigWeakReference) (ref)).id); } } } public static void main(String[] args) { int size = 3; LinkedList<WeakReference<VeryBig>> weakList = new LinkedList<>(); for (int i = 0; i < size; i++) { weakList.add(new VeryBigWeakReference(new VeryBig("Weak " + i), rq)); System.out.println("Just created weak: " + weakList.getLast()); } System.gc(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } checkQueue(); } } class VeryBig { public String id; Byte [] b = new byte[2 * 1024]; public VeryBig(String id) { this.id = id; } @Override protected void finalize() { System.out.println("Finalizing VeryBig " + id); } } class VeryBigWeakReference extends WeakReference<VeryBig> { public String id; public VeryBigWeakReference(VeryBig big, ReferenceQueue<VeryBig> rq) { super(big, rq); this.id = big.id; } @Override protected void finalize() { System.out.println("Finalizing VeryBigWeakReference " + id); }}Copy the code
- The output
Just created weak: cn.edu.cqvie.gc.VeryBigWeakReference@5451c3a8
Just created weak: cn.edu.cqvie.gc.VeryBigWeakReference@2626b418
Just created weak: cn.edu.cqvie.gc.VeryBigWeakReference@5a07e868
Finalizing VeryBig Weak 2
Finalizing VeryBig Weak 1
Finalizing VeryBig Weak 0
In queue:Weak 0
In queue:Weak 1
In queue:Weak 2
Copy the code
Phantom reference
- Virtual references are called ghost references or phantom references. It is the weakest reference relationship, and the existence of a virtual reference does not affect the lifetime of an exclusive object, nor can the object instance be obtained through a virtual reference. The only purpose of setting a virtual application association for an object is to receive a system notification when the object is reclaimed by the collector. After JDK1.2, phantomreferences were provided to implement virtual references.
The difference between the four citations
The differences between the four citations are as follows
Reference types | GC recovery time | use | Time to live |
---|---|---|---|
Strong reference | never | The general state of the object | The JVM stops running |
Soft references | Out of memory | Object caching | Terminates when memory is sufficient |
A weak reference | When the GC | Object caching | Terminated after GC |
Phantom reference | unknow | unknow | unknow |