java_强、软、弱、虚四大引用
1、强引用(默认使用)
//强引用 public class StrongReferenceDemo { public static void main(String[] args) { Object obj1 = new Object();//这样定义的,默认就是强引用 Object obj2 = obj1; obj1 = null; System.gc();//手动启动GC System.out.println(obj2); } }
2、软引用
import java.lang.ref.SoftReference; /** * 软引用:内存够用,不回收,内存不够就回收; */ public class SoftReferenceDemo { public static void main(String[] args) { //softRef_Memory_Enough(); softRef_Memory_NotEnough(); } //内存够用 private static void softRef_Memory_Enough() { Object o1 = new Object(); SoftReference<Object> soft = new SoftReference<>(o1); System.out.println(o1); System.out.println(soft.get()); o1 = null; System.gc();//手动GC System.out.println(o1); System.out.println(soft.get()); } /** * 内存不够用,自动GC,故意产生大对象并配置小内存,让它内存不够用导致OOM, * 看软引用的回收情况 */ private static void softRef_Memory_NotEnough() { Object o1 = new Object(); SoftReference<Object> soft = new SoftReference<>(o1); System.out.println(o1); System.out.println(soft.get()); o1 = null; try { byte[] bytes = new byte[30 * 1024 * 1024]; } catch (Exception e) { e.printStackTrace(); }finally { System.out.println(o1); System.out.println(soft.get()); } } }
3、弱引用
内存不管够不够,只要gc,就会回收!
import java.lang.ref.WeakReference; /** * 弱引用 只要GC,就会回收 */ public class WeakReferenceDemo { public static void main(String[] args) { Object o1 = new Object(); WeakReference<Object> weak = new WeakReference<>(o1); System.out.println(o1); System.out.println(weak.get()); o1 = null; System.gc(); System.out.println(o1); System.out.println(weak.get()); } }
WeakHashMap
import java.util.HashMap; import java.util.WeakHashMap; public class WeakHashMapDemo { public static void main(String[] args) { myHashMap(); System.out.println("======"); myWeakHashMap(); } private static void myHashMap() { HashMap<Integer, String> map = new HashMap<>(); Integer key = new Integer(1); String value = "HashMap"; map.put(key,value); System.out.println(map); key = null; System.out.println(map); System.gc(); System.out.println(map); } private static void myWeakHashMap() { WeakHashMap<Integer, String> map = new WeakHashMap<>(); Integer key = new Integer(1); String value = "HashMap"; map.put(key,value); System.out.println(map); key = null; System.out.println(map); System.gc(); System.out.println(map); } }
4、虚引用
import java.lang.ref.ReferenceQueue; import java.lang.ref.WeakReference; public class ReferenceQueueDemo { public static void main(String[] args)throws Exception { Object o1 = new Object(); ReferenceQueue<Object> queue = new ReferenceQueue<>(); WeakReference<Object> weak = new WeakReference<>(o1,queue); System.out.println(o1); System.out.println(weak.get()); System.out.println(queue.poll()); System.out.println("-====="); o1 = null; System.gc(); Thread.sleep(500); System.out.println(o1); System.out.println(weak.get()); System.out.println(queue.poll()); } }
虚引用
import java.lang.ref.PhantomReference; import java.lang.ref.ReferenceQueue; /** * 虚引用 */ public class PhantomReferenceDemo { public static void main(String[] args)throws Exception { Object o1 = new Object(); ReferenceQueue<Object> queue = new ReferenceQueue<>(); PhantomReference<Object> phantom = new PhantomReference<>(o1,queue); System.out.println(o1); System.out.println(phantom.get()); System.out.println(queue.poll()); System.out.println("==="); o1 = null; System.gc(); Thread.sleep(500); System.out.println(o1); System.out.println(phantom.get()); System.out.println(queue.poll()); } }