package jdk;import java.lang.ref.PhantomReference;import java.lang.ref.ReferenceQueue;import java.lang.ref.SoftReference;import java.lang.ref.WeakReference;public class SoftWeakPhantomReferenceTest { private static Man man = null; public static void main(String[] args) throws Exception { //testStringWeakReference(); //System.out.println("************************"); //testRealObjWeakReference(); //testRealObjSoftReference(); //testRealObjPhantomReference(); testRealObjPhantomReferenceWithThisEscape(); } public static void testStringWeakReference() throws Exception { String name = new String("why"); ReferenceQueuenameQueue = new ReferenceQueue<>(); WeakReference weakReference = new WeakReference (name, nameQueue); System.out.println("weakReference.isEnqueued-1:" + weakReference.isEnqueued()); name = null; System.out.println("name此时是可恢复状态:" + weakReference.get()); System.gc(); Thread.sleep(3000); System.out.println("name此时已经被回收了:" + weakReference.get()); System.out.println("weakReference.isEnqueued-2:" + weakReference.isEnqueued()); System.out.println("nameQueue = " + nameQueue.poll().get()); } public static void testRealObjSoftReference() throws Exception { byte[] byteArr = new byte[1024 * 1024 * 5]; System.out.println(byteArr.length); ReferenceQueue byteArrQueue = new ReferenceQueue (); SoftReference softReference = new SoftReference (byteArr, byteArrQueue); System.out.println("weakReference.isEnqueued-1:" + softReference.isEnqueued()); byteArr = null; System.out.println("byteArr此时是可恢复状态:" + softReference.get().length); new Thread(() -> { try { System.out.println("weakReference.isEnqueued-2:" + softReference.isEnqueued()); byteArrQueue.remove(); System.out.println("垃圾收集已执行"); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); byte[] byteArr2 = new byte[1024 * 1024 * 5]; System.out.println(byteArr2.length); System.gc(); Thread.sleep(3000); System.out.println("byteArr此时已经被回收了:" + softReference.get()); System.out.println("weakReference.isEnqueued-3:" + softReference.isEnqueued()); System.out.println("nameQueue = " + (byteArrQueue.poll() == null ? "null" : byteArrQueue.poll().get())); } public static void testRealObjWeakReference() throws Exception { Person person = new Person("why"); ReferenceQueue personQueue = new ReferenceQueue (); WeakReference weakReference = new WeakReference (person, personQueue); person = null; System.out.println("name此时是可恢复状态:" + weakReference.get()); new Thread(() -> { try { personQueue.remove(); System.out.println("垃圾收集已执行"); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); System.gc(); Thread.sleep(1000); System.out.println("personQueue.getClass() = " + personQueue.getClass().getName()); System.out.println("name此时已经被回收了:" + weakReference.get()); System.out.println("weakReference.isEnqueued:" + weakReference.isEnqueued()); System.out.println("nameQueue = " + (personQueue.poll() == null ? "null" : personQueue.poll().get())); } public static void testRealObjPhantomReference() throws Exception { Person person = new Person("why"); ReferenceQueue personQueue = new ReferenceQueue (); PhantomReference phantomReference = new PhantomReference (person, personQueue); System.out.println("weakReference.isEnqueued-1:" + phantomReference.isEnqueued()); person = null; System.out.println("name此时是可恢复状态:" + phantomReference.get()); new Thread(() -> { try { System.out.println("weakReference.isEnqueued-2:" + phantomReference.isEnqueued()); personQueue.remove(); System.out.println("垃圾收集已执行"); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); System.gc(); Thread.sleep(2000); System.out.println("name此时已经被回收了:" + phantomReference.get()); System.out.println("phantomReference.isEnqueued-3:" + phantomReference.isEnqueued()); System.out.println("nameQueue = " + (personQueue.poll() == null ? "null" : personQueue.poll().get())); } public static void testRealObjPhantomReferenceWithThisEscape() throws Exception { Man person = new Man("why"); ReferenceQueue personQueue = new ReferenceQueue (); PhantomReference phantomReference = new PhantomReference (person, personQueue); System.out.println("weakReference.isEnqueued-1:" + phantomReference.isEnqueued()); person = null; System.out.println("name此时是可恢复状态:" + phantomReference.get()); new Thread(() -> { try { System.out.println("weakReference.isEnqueued-2:" + phantomReference.isEnqueued()); personQueue.remove(); System.out.println("垃圾收集已执行"); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); System.gc(); Thread.sleep(2000); System.out.println("name此时已经被回收了:" + phantomReference.get()); System.out.println("phantomReference.isEnqueued-3:" + phantomReference.isEnqueued()); System.out.println("nameQueue = " + (personQueue.poll() == null ? "null" : personQueue.poll().get())); if (man != null) { System.out.println("man's name is " + man.getName()); } } static class Person { String name; public Person(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person [name=" + name + "]"; } } static class Man { String name; public Man(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override protected void finalize() throws Throwable { SoftWeakPhantomReferenceTest.man = this; System.out.println("this instance escapsed."); } @Override public String toString() { return "Person [name=" + name + "]"; } }}