Java强引用、软引用、弱引用、虚引用详解 (r)

 Java中没有指针的概念,而引用就是一个弱化的指针,保证开发不能任意操作内存。最近整理了一下之前不明白的各种级别引用:强引用、软引用、弱引用、虚引用,它们的特点和应用场景汇总如下:

1、强引用
    如果一个对象具有强引用,GC绝不会回收它;当内存空间不足,JVM宁愿抛出OutOfMemoryError错误。一般new出来的对象都是强引用,如下

  1. //强引用  
  2. User strangeReference=new User();  
//强引用
User strangeReference=new User();

      

2、软引用
     如果一个对象具有软引用,当内存空间不足,GC会回收这些对象的内存,使用软引用构建敏感数据的缓存。

     在JVM中,软引用是如下定义的,可以通过一个时间戳来回收,下面引自JVM:

  1. public class SoftReference<T> extends Reference<T> {  
  2.   
  3.     /** 
  4.      * Timestamp clock, updated by the garbage collector 
  5.      */  
  6.     static private long clock;  
  7.   
  8.     /** 
  9.      * Timestamp updated by each invocation of the get method.  The VM may use 
  10.      * this field when selecting soft references to be cleared, but it is not 
  11.      * required to do so. 
  12.      */  
  13.     private long timestamp;  
  14.   
  15.     /** 
  16.      * Creates a new soft reference that refers to the given object.  The new 
  17.      * reference is not registered with any queue. 
  18.      * 
  19.      * @param referent object the new soft reference will refer to 
  20.      */  
  21.     public SoftReference(T referent) {  
  22.         super(referent);  
  23.         this.timestamp = clock;  
  24.     }  
  25.   
  26.     /** 
  27.      * Creates a new soft reference that refers to the given object and is 
  28.      * registered with the given queue. 
  29.      * 
  30.      * @param referent object the new soft reference will refer to 
  31.      * @param q the queue with which the reference is to be registered, 
  32.      *          or <tt>null</tt> if registration is not required 
  33.      * 
  34.      */  
  35.     public SoftReference(T referent, ReferenceQueue<? super T> q) {  
  36.         super(referent, q);  
  37.         this.timestamp = clock;  
  38.     }  
  39.   
  40.     /** 
  41.      * Returns this reference object's referent.  If this reference object has 
  42.      * been cleared, either by the program or by the garbage collector, then 
  43.      * this method returns <code>null</code>. 
  44.      * 
  45.      * @return   The object to which this reference refers, or 
  46.      *           <code>null</code> if this reference object has been cleared 
  47.      */  
  48.     public T get() {  
  49.         T o = super.get();  
  50.         if (o != null && this.timestamp != clock)  
  51.             this.timestamp = clock;  
  52.         return o;  
  53.     }  
  54.   
  55. }  
public class SoftReference<T> extends Reference<T> {

    /**
     * Timestamp clock, updated by the garbage collector
     */
    static private long clock;

    /**
     * Timestamp updated by each invocation of the get method.  The VM may use
     * this field when selecting soft references to be cleared, but it is not
     * required to do so.
     */
    private long timestamp;

    /**
     * Creates a new soft reference that refers to the given object.  The new
     * reference is not registered with any queue.
     *
     * @param referent object the new soft reference will refer to
     */
    public SoftReference(T referent) {
        super(referent);
        this.timestamp = clock;
    }

    /**
     * Creates a new soft reference that refers to the given object and is
     * registered with the given queue.
     *
     * @param referent object the new soft reference will refer to
     * @param q the queue with which the reference is to be registered,
     *          or <tt>null</tt> if registration is not required
     *
     */
    public SoftReference(T referent, ReferenceQueue<? super T> q) {
        super(referent, q);
        this.timestamp = clock;
    }

    /**
     * Returns this reference object's referent.  If this reference object has
     * been cleared, either by the program or by the garbage collector, then
     * this method returns <code>null</code>.
     *
     * @return   The object to which this reference refers, or
     *           <code>null</code> if this reference object has been cleared
     */
    public T get() {
        T o = super.get();
        if (o != null && this.timestamp != clock)
            this.timestamp = clock;
        return o;
    }

}

   软引用的声明的借助强引用或者匿名对象,使用泛型SoftReference<T>;可以通过get方法获得强引用。具体如下:

  1. //软引用  
  2. SoftReference<User>softReference=new SoftReference<User>(new User());  
  3. strangeReference=softReference.get();//通过get方法获得强引用  
//软引用
SoftReference<User>softReference=new SoftReference<User>(new User());
strangeReference=softReference.get();//通过get方法获得强引用


3、弱引用  
     如果一个对象具有弱引用,在GC线程扫描内存区域的过程中,不管当前内存空间足够与否,都会回收内存,使用弱引用 构建非敏感数据的缓存。

     在JVM中,弱引用是如下定义的,下面引自JVM:

  1. public class WeakReference<T> extends Reference<T> {  
  2.   
  3.     /** 
  4.      * Creates a new weak reference that refers to the given object.  The new 
  5.      * reference is not registered with any queue. 
  6.      * 
  7.      * @param referent object the new weak reference will refer to 
  8.      */  
  9.     public WeakReference(T referent) {  
  10.         super(referent);  
  11.     }  
  12.   
  13.     /** 
  14.      * Creates a new weak reference that refers to the given object and is 
  15.      * registered with the given queue. 
  16.      * 
  17.      * @param referent object the new weak reference will refer to 
  18.      * @param q the queue with which the reference is to be registered, 
  19.      *          or <tt>null</tt> if registration is not required 
  20.      */  
  21.     public WeakReference(T referent, ReferenceQueue<? super T> q) {  
  22.         super(referent, q);  
  23.     }  
  24.   
  25. }  
public class WeakReference<T> extends Reference<T> {

    /**
     * Creates a new weak reference that refers to the given object.  The new
     * reference is not registered with any queue.
     *
     * @param referent object the new weak reference will refer to
     */
    public WeakReference(T referent) {
        super(referent);
    }

    /**
     * Creates a new weak reference that refers to the given object and is
     * registered with the given queue.
     *
     * @param referent object the new weak reference will refer to
     * @param q the queue with which the reference is to be registered,
     *          or <tt>null</tt> if registration is not required
     */
    public WeakReference(T referent, ReferenceQueue<? super T> q) {
        super(referent, q);
    }

}


    弱引用的声明的借助强引用或者匿名对象,使用泛型WeakReference<T>,具体如下:

  1. //弱引用  
  2. WeakReference<User>weakReference=new WeakReference<User>(new User());  
//弱引用
WeakReference<User>weakReference=new WeakReference<User>(new User());


4、虚引用
     如果一个对象仅持有虚引用,在任何时候都可能被垃圾回收,虚引用与软引用和弱引用的一个区别在于:虚引用必须和引用队列联合使用,虚引用主要用来跟踪对象 被垃圾回收的活动。

     在JVM中,虚引用是如下定义的,下面引自JVM:

  1. public class PhantomReference<T> extends Reference<T> {  
  2.   
  3.     /** 
  4.      * Returns this reference object's referent.  Because the referent of a 
  5.      * phantom reference is always inaccessible, this method always returns 
  6.      * <code>null</code>. 
  7.      * 
  8.      * @return  <code>null</code> 
  9.      */  
  10.     public T get() {  
  11.         return null;  
  12.     }  
  13.   
  14.     /** 
  15.      * Creates a new phantom reference that refers to the given object and 
  16.      * is registered with the given queue. 
  17.      * 
  18.      * <p> It is possible to create a phantom reference with a <tt>null</tt> 
  19.      * queue, but such a reference is completely useless: Its <tt>get</tt> 
  20.      * method will always return null and, since it does not have a queue, it 
  21.      * will never be enqueued. 
  22.      * 
  23.      * @param referent the object the new phantom reference will refer to 
  24.      * @param q the queue with which the reference is to be registered, 
  25.      *          or <tt>null</tt> if registration is not required 
  26.      */  
  27.     public PhantomReference(T referent, ReferenceQueue<? super T> q) {  
  28.         super(referent, q);  
  29.     }  
  30.   
  31. }  
public class PhantomReference<T> extends Reference<T> {

    /**
     * Returns this reference object's referent.  Because the referent of a
     * phantom reference is always inaccessible, this method always returns
     * <code>null</code>.
     *
     * @return  <code>null</code>
     */
    public T get() {
        return null;
    }

    /**
     * Creates a new phantom reference that refers to the given object and
     * is registered with the given queue.
     *
     * <p> It is possible to create a phantom reference with a <tt>null</tt>
     * queue, but such a reference is completely useless: Its <tt>get</tt>
     * method will always return null and, since it does not have a queue, it
     * will never be enqueued.
     *
     * @param referent the object the new phantom reference will refer to
     * @param q the queue with which the reference is to be registered,
     *          or <tt>null</tt> if registration is not required
     */
    public PhantomReference(T referent, ReferenceQueue<? super T> q) {
        super(referent, q);
    }

}

     虚引用PhantomReference<T>的声明的借助强引用或者匿名对象,结合泛型ReferenceQueue<T>初始化,具体如下:

  1. //虚引用  
  2. PhantomReference<User> phantomReference=new PhantomReference<User>(new User(),new ReferenceQueue<User>());  
//虚引用
PhantomReference<User> phantomReference=new PhantomReference<User>(new User(),new ReferenceQueue<User>());


5、总结

  下面是一段关于强引用、软引用、弱引用、虚引用的程序:

  1. import java.lang.ref.*;  
  2. import java.util.HashSet;  
  3. import java.util.Set;  
  4.   
  5. class User {  
  6.   
  7.     private String name;  
  8.   
  9.     public User()  
  10.     {}  
  11.   
  12.     public User(String name)  
  13.     {  
  14.         this.name=name;  
  15.     }  
  16.   
  17.     @Override  
  18.     public String toString() {  
  19.         return name;  
  20.     }  
  21.   
  22.     public void finalize(){  
  23.         System.out.println("Finalizing ... "+name);  
  24.     }  
  25. }  
  26.   
  27. /** 
  28.  * Created by jinxu on 15-4-25. 
  29.  */  
  30. public class ReferenceDemo {  
  31.   
  32.     private static ReferenceQueue<User> referenceQueue = new ReferenceQueue<User>();  
  33.     private static final int size = 10;  
  34.   
  35.     public static void checkQueue(){  
  36.        /* Reference<? extends User> reference = null; 
  37.         while((reference = referenceQueue.poll())!=null){ 
  38.             System.out.println("In queue : "+reference.get()); 
  39.         }*/  
  40.         Reference<? extends User> reference = referenceQueue.poll();  
  41.         if(reference!=null){  
  42.             System.out.println("In queue : "+reference.get());  
  43.         }  
  44.     }  
  45.   
  46.     public static void testSoftReference()  
  47.     {  
  48.         Set<SoftReference<User>> softReferenceSet = new HashSet<SoftReference<User>>();  
  49.         for (int i = 0; i < size; i++) {  
  50.             SoftReference<User> ref = new SoftReference<User>(new User("Soft " + i), referenceQueue);  
  51.             System.out.println("Just created: " + ref.get());  
  52.             softReferenceSet.add(ref);  
  53.         }  
  54.         System.gc();  
  55.         checkQueue();  
  56.     }  
  57.   
  58.     public static void testWeaKReference()  
  59.     {  
  60.         Set<WeakReference<User>> weakReferenceSet = new HashSet<WeakReference<User>>();  
  61.         for (int i = 0; i < size; i++) {  
  62.             WeakReference<User> ref = new WeakReference<User>(new User("Weak " + i), referenceQueue);  
  63.             System.out.println("Just created: " + ref.get());  
  64.             weakReferenceSet.add(ref);  
  65.         }  
  66.         System.gc();  
  67.         checkQueue();  
  68.     }  
  69.   
  70.     public static void testPhantomReference()  
  71.     {  
  72.         Set<PhantomReference<User>> phantomReferenceSet = new HashSet<PhantomReference<User>>();  
  73.         for (int i = 0; i < size; i++) {  
  74.             PhantomReference<User> ref =  
  75.                     new PhantomReference<User>(new User("Phantom " + i), referenceQueue);  
  76.             System.out.println("Just created: " + ref.get());  
  77.             phantomReferenceSet.add(ref);  
  78.         }  
  79.         System.gc();  
  80.         checkQueue();  
  81.     }  
  82.   
  83.     public static void main(String[] args) {  
  84.         testSoftReference();  
  85.         testWeaKReference();  
  86.         testPhantomReference();  
  87.     }  
  88. }  
import java.lang.ref.*;
import java.util.HashSet;
import java.util.Set;

class User {

    private String name;

    public User()
    {}

    public User(String name)
    {
        this.name=name;
    }

    @Override
    public String toString() {
        return name;
    }

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

/**
 * Created by jinxu on 15-4-25.
 */
public class ReferenceDemo {

    private static ReferenceQueue<User> referenceQueue = new ReferenceQueue<User>();
    private static final int size = 10;

    public static void checkQueue(){
       /* Reference<? extends User> reference = null;
        while((reference = referenceQueue.poll())!=null){
            System.out.println("In queue : "+reference.get());
        }*/
        Reference<? extends User> reference = referenceQueue.poll();
        if(reference!=null){
            System.out.println("In queue : "+reference.get());
        }
    }

    public static void testSoftReference()
    {
        Set<SoftReference<User>> softReferenceSet = new HashSet<SoftReference<User>>();
        for (int i = 0; i < size; i++) {
            SoftReference<User> ref = new SoftReference<User>(new User("Soft " + i), referenceQueue);
            System.out.println("Just created: " + ref.get());
            softReferenceSet.add(ref);
        }
        System.gc();
        checkQueue();
    }

    public static void testWeaKReference()
    {
        Set<WeakReference<User>> weakReferenceSet = new HashSet<WeakReference<User>>();
        for (int i = 0; i < size; i++) {
            WeakReference<User> ref = new WeakReference<User>(new User("Weak " + i), referenceQueue);
            System.out.println("Just created: " + ref.get());
            weakReferenceSet.add(ref);
        }
        System.gc();
        checkQueue();
    }

    public static void testPhantomReference()
    {
        Set<PhantomReference<User>> phantomReferenceSet = new HashSet<PhantomReference<User>>();
        for (int i = 0; i < size; i++) {
            PhantomReference<User> ref =
                    new PhantomReference<User>(new User("Phantom " + i), referenceQueue);
            System.out.println("Just created: " + ref.get());
            phantomReferenceSet.add(ref);
        }
        System.gc();
        checkQueue();
    }

    public static void main(String[] args) {
        testSoftReference();
        testWeaKReference();
        testPhantomReference();
    }
}

     结果为

  1. Just created: Soft 0  
  2. Just created: Soft 1  
  3. Just created: Soft 2  
  4. Just created: Soft 3  
  5. Just created: Soft 4  
  6. Just created: Soft 5  
  7. Just created: Soft 6  
  8. Just created: Soft 7  
  9. Just created: Soft 8  
  10. Just created: Soft 9  
  11. Just created: Weak 0  
  12. Just created: Weak 1  
  13. Just created: Weak 2  
  14. Just created: Weak 3  
  15. Just created: Weak 4  
  16. Just created: Weak 5  
  17. Just created: Weak 6  
  18. Just created: Weak 7  
  19. Just created: Weak 8  
  20. Just created: Weak 9  
  21. Finalizing ... Weak 7  
  22. Finalizing ... Weak 8  
  23. Finalizing ... Weak 9  
  24. Finalizing ... Weak 4  
  25. Finalizing ... Weak 5  
  26. Finalizing ... Weak 6  
  27. Finalizing ... Weak 0  
  28. Finalizing ... Weak 1  
  29. Finalizing ... Weak 2  
  30. Finalizing ... Weak 3  
  31. Finalizing ... Soft 9  
  32. Finalizing ... Soft 8  
  33. Finalizing ... Soft 7  
  34. Finalizing ... Soft 6  
  35. Finalizing ... Soft 5  
  36. Finalizing ... Soft 4  
  37. Finalizing ... Soft 3  
  38. Finalizing ... Soft 2  
  39. Finalizing ... Soft 1  
  40. Finalizing ... Soft 0  
  41. In queue : null  
  42. Just created: null  
  43. Just created: null  
  44. Just created: null  
  45. Just created: null  
  46. Just created: null  
  47. Just created: null  
  48. Just created: null  
  49. Just created: null  
  50. Just created: null  
  51. Just created: null  
  52. In queue : null  
  53. Finalizing ... Phantom 9  
  54. Finalizing ... Phantom 7  
  55. Finalizing ... Phantom 8  
  56. Finalizing ... Phantom 4  
  57. Finalizing ... Phantom 5  
  58. Finalizing ... Phantom 6  
  59. Finalizing ... Phantom 0  
  60. Finalizing ... Phantom 1  
  61. Finalizing ... Phantom 2  
  62. Finalizing ... Phantom 3  
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
Finalizing ... Weak 7
Finalizing ... Weak 8
Finalizing ... Weak 9
Finalizing ... Weak 4
Finalizing ... Weak 5
Finalizing ... Weak 6
Finalizing ... Weak 0
Finalizing ... Weak 1
Finalizing ... Weak 2
Finalizing ... Weak 3
Finalizing ... Soft 9
Finalizing ... Soft 8
Finalizing ... Soft 7
Finalizing ... Soft 6
Finalizing ... Soft 5
Finalizing ... Soft 4
Finalizing ... Soft 3
Finalizing ... Soft 2
Finalizing ... Soft 1
Finalizing ... Soft 0
In queue : 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
Just created: null
In queue : null
Finalizing ... Phantom 9
Finalizing ... Phantom 7
Finalizing ... Phantom 8
Finalizing ... Phantom 4
Finalizing ... Phantom 5
Finalizing ... Phantom 6
Finalizing ... Phantom 0
Finalizing ... Phantom 1
Finalizing ... Phantom 2
Finalizing ... Phantom 3



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


         由于时间有限,在写博文的过程中参考过一些文献,在此表示感谢;同时鉴于水平原因,你难免有不足之处,欢迎斧正!


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值