Java中软引用、弱引用和虚引用的使用方法示例

在Java引用中,使用最多的无疑是强引用类型,对于其余三种级别的引用类型用得比较少。它们使用的方式与强引用也有些区别。下面使用一个例子来介绍一下其余三种引用(包括软引用、弱引用和虚引用)的使用方法。 

Java代码   收藏代码
  1. import java.lang.ref.*;  
  2. import java.util.*;  
  3.   
  4. class Grocery {  
  5.         private static final int SIZE=10000;  
  6.         private double[] d = new double[SIZE];  
  7.         private String id;  
  8.   
  9.         public Grocery(String id) {  
  10.                 this.id = id;  
  11.         }  
  12.   
  13.         public String toString(){  
  14.                 return id;  
  15.         }  
  16.   
  17.         public void finalize(){  
  18.                 System.out.println("Finalizing ... "+id);  
  19.         }  
  20.   
  21. }  
  22.   
  23. public class References {  
  24.         private static ReferenceQueue<Grocery> rq = new ReferenceQueue<Grocery>();  
  25.   
  26.         public static void checkQueue(){  
  27.                 Reference<? extends Grocery> inq = rq.poll();  
  28.                 if(inq!=null){  
  29.                         System.out.println("In queue : "+inq.get());  
  30.                 }  
  31.         }  
  32.   
  33.         public static void main(String[] args){  
  34.                 final int size = 10;  
  35.                 Set<SoftReference<Grocery>> sa = new HashSet<SoftReference<Grocery>>();  
  36.   
  37.                 for(int i=0;i<size;i++){  
  38.                         SoftReference<Grocery> ref =  
  39.                                 new SoftReference<Grocery>(new Grocery("Soft "+i),rq);  
  40.                         System.out.println("Just created: "+ref.get());  
  41.                         sa.add(ref);  
  42.                 }  
  43.   
  44.                 System.gc();  
  45.                 checkQueue();  
  46.   
  47.                 Set<WeakReference<Grocery>> wa = new HashSet<WeakReference<Grocery>>();  
  48.   
  49.                 for(int i=0;i<size;i++){  
  50.                         WeakReference<Grocery> ref =  
  51.                                         new WeakReference<Grocery>(new Grocery("Weak "+i),rq);  
  52.                         System.out.println("Just created: "+ref.get());  
  53.                         wa.add(ref);  
  54.                 }  
  55.   
  56.                 System.gc();  
  57.                 checkQueue();  
  58.   
  59.                 Set<PhantomReference<Grocery>> pa = new HashSet<PhantomReference<Grocery>>();  
  60.   
  61.                 for(int i=0;i<size;i++) {  
  62.                         PhantomReference<Grocery> ref =  
  63.                                 new PhantomReference<Grocery>(new Grocery("Phantom "+i),rq);  
  64.                         System.out.println("Just created: "+ref.get());  
  65.                         pa.add(ref);  
  66.                 }  
  67.   
  68.                 System.gc();  
  69.                 checkQueue();  
  70.         }  
  71. }  



在上面的程序中,References类依次创建了10个软引用、10个弱引用和10个虚引用,它们各自引用一个Grocery对象。下面是运行结果: 

Lab-Computer-0db2f6:JavaExercises labuser$ javac References.java 
Lab-Computer-0db2f6:JavaExercises labuser$ java References 
Just created: Soft 0 
Just created: Soft 1 
Just created: Soft 2 
Just created: Soft 3 
Just created: Soft 4 
Just created: Soft 5 
Just created: Soft 6 
Just created: Soft 7 
Just created: Soft 8 
Just created: Soft 9 
Just created: Weak 0 
Just created: Weak 1 
Just created: Weak 2 
Just created: Weak 3 
Just created: Weak 4 
Just created: Weak 5 
Just created: Weak 6 
Just created: Weak 7 
Just created: Weak 8 
Just created: Weak 9 
Just created: null 
Just created: null 
Just created: null 
Just created: null 
Just created: null 
Just created: null 
Just created: null 
Just created: null 
Just created: null 
Just created: null 
Finalizing ... Weak 9 
Finalizing ... Weak 8 
Finalizing ... Weak 7 
Finalizing ... Weak 6 
Finalizing ... Weak 5 
Finalizing ... Weak 4 
Finalizing ... Weak 3 
Finalizing ... Weak 2 
Finalizing ... Weak 1 
Finalizing ... Weak 0 
Finalizing ... Phantom 9 
Finalizing ... Phantom 8 
Finalizing ... Phantom 7 
Finalizing ... Phantom 6 
Finalizing ... Phantom 5 
Finalizing ... Phantom 4 
Finalizing ... Phantom 3 
Finalizing ... Phantom 2 
Finalizing ... Phantom 1 
Finalizing ... Phantom 0 


从程序运行结果可以看出,虚引用形同虚设,它所引用的对象随时可能被垃圾回收器回收,具有弱引用的对象拥有稍微长一点的生命周期,当垃圾回收器执行回收操作时,有可能被垃圾回收器回收,具有软引用的对象拥有更长的生命周期,但在Java虚拟机认为内存不足的情况下,也是会被垃圾回收器回收的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值