current position:Home>Four reference types in Java

Four reference types in Java

2022-06-24 09:04:11Flowers and rain

1. Strong citation

stay Java The most common is the strong quotation , Assign an object to a reference variable , This reference variable is a strong reference . When one
When an object is referenced by a strongly referenced variable , It's reachable , It can't be recycled by garbage collection mechanism , Even if the object will never
Will not be used JVM It won't recycle . So strong citations are the cause of Java One of the main causes of memory leaks .

M m = new M();
m = null;
System.gc();
System.out.println(sr.get()); // null

2. Soft citation

Soft quotes need to use SoftReference Class to achieve , For objects with only soft references , When the system has enough memory, it will not be returned
closed , When the system runs out of memory, it is recycled . Soft references are often used in memory sensitive programs .

SoftReference<byte[]> sr = new SoftReference<>(new byte[1024 * 1024 * 10]);
System.out.println(sr.get()); //  Address 
System.gc();
System.out.println(sr.get()); //  Address 
byte[] b = new byte[1024 * 1024 * 12];
System.out.println(sr.get()); // null

3. Weak reference

Weak references need to use WeakReference Class to achieve , It has a shorter lifetime than soft quotes , For objects with only weak references , only
Once the garbage collection mechanism is running , No matter JVM Is there enough memory space for , Always reclaim the memory occupied by this object .【ThreadLocal】

WeakReference<M> wr = new WeakReference<>(new M());
System.out.println(wr.get()); //  Get value 
System.gc();
SleepHelper.sleepSeconds(1);
System.out.println(wr.get()); // null

4. Virtual reference

Virtual references require PhantomReference Class to achieve , It can't be used alone , Must be used in conjunction with the reference queue . Main of virtual reference
The function is to track the status of the object being garbage collected .【NIO】

private static final List<Object> LIST = new LinkedList<>();
private static final ReferenceQueue<M> QUEUE = new ReferenceQueue<>();
public static void main(String[] args) {
    
	PhantomReference<M> phantomReference = new PhantomReference<>(new M(), QUEUE);
	System.out.println(phantomReference.get()); //null
	ByteBuffer b = ByteBuffer.allocateDirect(1024);
	new Thread(()->{
    
		while(true){
    
			LIST.add(new byte[1024 * 1024]);
			SleepHelper.sleepSeconds(1);
			System.out.println(phantomReference.get());
		}
		//  After running out of memory ,M Objects will be recycled , It will be put into QUEUE in 
	}).start();
	new Thread(()->{
    
		while(true){
    
			Reference<? extends M> poll = QUEUE.poll();
			if(poll != null) {
    
				System.out.println("--- Virtual reference object is JVM Recycled ---" + poll);
			}
		}
	}).start();
}

copyright notice
author[Flowers and rain],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/175/202206240717100184.html

Random recommended