JAVA的四种引用


转自:http://kinkding.iteye.com/blog/403490


在论坛里面看到了帖子:http://www.iteye.com/topic/401478,下面是我对四种引用的试用:

1、强引用:

 

Java代码   收藏代码
  1. /** 强引用,JVM的默认实现 */  
  2. public static void test1() throws InterruptedException {  
  3.     Object obj = new Object();  
  4.     Object strong = obj;  
  5.     obj = null;  
  6.     System.gc();  
  7.     Thread.sleep(1000);  
  8.     System.out.println(strong);  
  9. }  

 输出如下:

java.lang.Object@35ce36

 

2、弱引用:

 

Java代码   收藏代码
  1. /** 
  2.  * WeakReference 弱引用( 当所引用的对象在 JVM 内不再有强引用时, GC 后weak reference 将会被自动回收) 
  3.  * */  
  4. public static void test2() throws InterruptedException {  
  5.     Object obj = new Object();  
  6.     WeakReference<Object> wr = new WeakReference<Object>(obj);  
  7.     obj = null;  
  8.     System.gc();  
  9.     Thread.sleep(1000);  
  10.     System.out.println(wr.get());  
  11. }  

 输出如下:

null

 

3、软引用:

 

Java代码   收藏代码
  1. /** 
  2.  * SoftReference SoftReference 于 WeakReference 的特性基本一致, 最大的区别在于 
  3.  * SoftReference 会尽可能长的保留引用直到 JVM 内存不足时才会被回收(虚拟机保证) 
  4.  * */  
  5. public static void test3() throws InterruptedException {  
  6.     Object obj = new Object();  
  7.     SoftReference<Object> sr = new SoftReference<Object>(obj);  
  8.     obj = null;  
  9.     System.gc();  
  10.     Thread.sleep(1000);  
  11.     System.out.println(sr.get());  
  12. }  

 输出如下:

java.lang.Object@35ce36

 

4、幽灵引用:

 

Java代码   收藏代码
  1. /** 
  2.  * PhantomReference Phantom Reference(幽灵引用) 与 WeakReference 和 SoftReference 
  3.  * 有很大的不同, 因为它的 get() 方法永远返回 null 
  4.  * */  
  5. public static void test4() throws InterruptedException {  
  6.     Object obj = new Object();  
  7.     ReferenceQueue<Object> rq = new ReferenceQueue<Object>();  
  8.     PhantomReference<Object> pr = new PhantomReference<Object>(obj, rq);  
  9.     System.out.println(pr.get());  
  10. }  

 输出如下:

null

 

5、ReferenceQueue:

 

Java代码   收藏代码
  1. public static void test5() throws InterruptedException {  
  2.     Object obj = new Object();  
  3.     ReferenceQueue<Object> rq = new ReferenceQueue<Object>();  
  4.     WeakReference<Object> pr = new WeakReference<Object>(obj, rq);  
  5.     System.out.println(pr.isEnqueued());  
  6.     System.out.println(rq.poll());  
  7.     obj = null;  
  8.     System.gc();  
  9.     System.out.println(pr.isEnqueued());  
  10.     System.out.println(rq.remove().get());  
  11. }  

 输出如下:

 

false

null

true

null

 

6、WeakHashMap:

 

Java代码   收藏代码
  1. /** 
  2.  * 使用 WeakReference 作为 key, 一旦没有指向 key 的强引用,  
  3.  * WeakHashMap 在 GC 后将自动删除相关的 
  4.  * entry 
  5.  */  
  6. public static void test6() throws InterruptedException {  
  7.     Map<Object, Object> map = new WeakHashMap<Object, Object>();  
  8.     Object key = new Object();  
  9.     Object value = new Object();  
  10.     map.put(key, value);  
  11.   
  12.     key = null;  
  13.     System.gc();  
  14.     Thread.sleep(1000);  
  15.     System.out.println(value);  
  16.     System.out.println(map.containsValue(value));  
  17. }  

 输出如下:

 

java.lang.Object@757aef

false









  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值