欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-14 22:36:23
...
在Java引用中,使用最多的无疑是强引用类型,对于其余三种级别的引用类型用得比较少。它们使用的方式与强引用也有些区别。下面使用一个例子来介绍一下其余三种引用(包括软引用、弱引用和虚引用)的使用方法。


import java.lang.ref.*;
import java.util.*;

class Grocery {
        private static final int SIZE=10000;
        private double[] d = new double[SIZE];
        private String id;

        public Grocery(String id) {
                this.id = id;
        }

        public String toString(){
                return id;
        }

        public void finalize(){
                System.out.println("Finalizing ... "+id);
        }

}

public class References {
        private static ReferenceQueue<Grocery> rq = new ReferenceQueue<Grocery>();

        public static void checkQueue(){
                Reference<? extends Grocery> inq = rq.poll();
                if(inq!=null){
                        System.out.println("In queue : "+inq.get());
                }
        }

        public static void main(String[] args){
                final int size = 10;
                Set<SoftReference<Grocery>> sa = new HashSet<SoftReference<Grocery>>();

                for(int i=0;i<size;i++){
                        SoftReference<Grocery> ref =
                                new SoftReference<Grocery>(new Grocery("Soft "+i),rq);
                        System.out.println("Just created: "+ref.get());
                        sa.add(ref);
                }

                System.gc();
                checkQueue();

                Set<WeakReference<Grocery>> wa = new HashSet<WeakReference<Grocery>>();

                for(int i=0;i<size;i++){
                        WeakReference<Grocery> ref =
                                        new WeakReference<Grocery>(new Grocery("Weak "+i),rq);
                        System.out.println("Just created: "+ref.get());
                        wa.add(ref);
                }

                System.gc();
                checkQueue();

                Set<PhantomReference<Grocery>> pa = new HashSet<PhantomReference<Grocery>>();

                for(int i=0;i<size;i++) {
                        PhantomReference<Grocery> ref =
                                new PhantomReference<Grocery>(new Grocery("Phantom "+i),rq);
                        System.out.println("Just created: "+ref.get());
                        pa.add(ref);
                }

                System.gc();
                checkQueue();
        }
}






在上面的程序中,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虚拟机认为内存不足的情况下,也是会被垃圾回收器回收的。