来源 《java性能优化》
1、强引用不会被系统回收,软引用当内存不足时才回收
2、弱引用只要执行gc()操作,对象就会被回收,弱软引用用来保存可有可无的数据
3、虚引用常用来跟踪对象垃圾回收过程,每次get()都会返回null,随时都会被垃圾回收
4、WeakHashMap使用弱引用,会自动释放已经被回收的key,但WeakHashMap保存的key不能被其他地方引用,否则自动清理时不会被回收。
< code_snippet_id="2429641" snippet_file_name="blog_20170601_1_8930707" name="code" class="java">import java.lang.ref.PhantomReference;import java.lang.ref.Reference;import java.lang.ref.ReferenceQueue;import java.lang.ref.SoftReference;import java.lang.ref.WeakReference;import java.util.HashMap;import org.junit.Test;/**强引用不会被系统回收*/public class TestRef { ReferenceQueue<MyObject> softQueue = null; public class CheckRefQueue extends Thread { @SupssWarnings("unchecked") @Override public void run() { while (true) { if (softQueue != null) { Reference<MyObject> obj = null; try { // Removes the next reference object in this queue, blocking until one becomes available. obj = (Reference<MyObject>) softQueue.remove(); System.out.println("Removes the next reference object in this softQueue"); } catch (InterruptedException e) { e.printStackTrace(); } if (obj != null) System.out.println("Object for SoftReference is " + obj.get()); } } } } // public class MemConsumer extends Thread{ // HashMap map=new HashMap(); // @Override // public void run(){ // for(int i=0;i<10000;i++){ // map.put(i, new StringBuffer().append(i)); // } // } // }// @Test public void test() throws InterruptedException { MyObject obj = new MyObject(); softQueue = new ReferenceQueue<MyObject>(); SoftReference<MyObject> softRef = new SoftReference<MyObject>(obj, softQueue);//当对象被回收时加入到softQueue中 new CheckRefQueue().start(); // ȥ��ǿ���� obj = null; System.gc(); System.out.println("After GC:Soft Get= " + softRef.get()); System.out.println("�������ڴ�"); byte[] b = new byte[4 * 1024 * 1000]; System.out.println("After new byte[]:Soft Get= " + softRef.get()); } /** -Xms5M ,软引用当内存不足时才回收 */// @Test public void testSimple() throws InterruptedException { MyObject obj = new MyObject(); SoftReference<MyObject> softRef = new SoftReference<MyObject>(obj); // ȥ��ǿ���� obj = null; System.out.println("Soft Get: " + softRef.get()); System.gc(); System.out.println("Soft Get: " + softRef.get()); byte[] b = new byte[4 * 1024 * 1000]; System.out.println("Soft Get: " + softRef.get()); } /**弱引用只要执行gc()操作,垃圾回收,对象就会被回收,弱软引用用来保存可有可无的数据*/// @Test public void test2() throws InterruptedException { MyObject obj = new MyObject(); softQueue = new ReferenceQueue<MyObject>(); WeakReference<MyObject> softRef = new WeakReference<MyObject>(obj,softQueue);//当对象被回收时加入到softQueue中 new CheckRefQueue().start(); // ȥ��ǿ���� obj = null; System.out.println("b GC:Soft Get= " + softRef.get()); System.gc(); System.out.println("After GC:Soft Get= " + softRef.get()); System.out.println("�������ڴ�"); byte[] b = new byte[4 * 1024 * 100]; System.out.println("After new byte[]:Soft Get= " + softRef.get()); } /**虚引用常用来跟踪对象垃圾回收过程,每次get()都会返回null,随时都会被垃圾回收*/// @Test public void test3() throws InterruptedException { MyObject obj = new MyObject(); softQueue = new ReferenceQueue<MyObject>(); PhantomReference<MyObject> softRef = new PhantomReference<MyObject>(obj,softQueue);//当对象被回收时加入到softQueue中 new CheckRefQueue().start(); // ȥ��ǿ���� obj = null; int i=0; while (true) { System.out.println("第几次"+i++); System.gc(); Thread.sleep(1000); } } @Test public void test4(){ } }class MyObject { @Override protected void finalize() throws Throwable { super.finalize(); System.out.println("MyObject's finalize called"); } @Override public String toString(){ return "I am MyObject"; }}>
< code_snippet_id="2429641" snippet_file_name="blog_20170601_2_7262267" name="code" class="java">import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.WeakHashMap;import org.junit.Test;/** * -Xmx5M * @author Administrator * */public class TestWeakHashMap { Map map; // @Test public void testWeakHashMap() { map=new WeakHashMap(); List l=new ArrayList(); for(int i=0;i<10000;i++){ Integer ii=new Integer(i); map.put(ii,new byte[i]); } } // @Test public void testWeakHashMap2() { map=new WeakHashMap(); List l=new ArrayList(); for(int i=0;i<10000;i++){ Integer ii=new Integer(i); l.add(ii); map.put(ii,new byte[i]); } } @Test public void testHashMap() { map=new HashMap(); for(int i=0;i<10000;i++){ Integer ii=new Integer(i); map.put(ii,new byte[i]); } }}>