javaee论坛

普通会员

225648

帖子

346

回复

360

积分

楼主
发表于 2017-06-19 05:12:57 | 查看: 104 | 回复: 4
来源 《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]); } }}


上一篇:i/o速度比较 下一篇:jstack和线程dump分析

普通会员

10

帖子

330

回复

359

积分
沙发
发表于 2022-05-07 16:02:43

好好好

普通会员

0

帖子

320

回复

326

积分
板凳
发表于 2022-12-28 10:23:58

谢谢楼主分享

普通会员

0

帖子

333

回复

346

积分
地板
发表于 2023-01-02 19:48:18

谢谢楼主分享

普通会员

0

帖子

297

回复

303

积分
4#
发表于 2024-01-03 16:05:36

楼主节操掉了,还不快捡起来

您需要登录后才可以回帖 登录 | 立即注册

触屏版| 电脑版

技术支持 历史网 V2.0 © 2016-2017