java Reference

1. 介绍

在java当中一切事物皆对象。那么每一个对象都在内存当中有自己的位置,当引用获取不得指向的对象的时候,java垃圾回收机制就会将此引用清理。
但经过Reference包装过的引用,根据包装的强度不同,在回收的时候也有不同的调度。

Reference子类:SoftReference,WeakReference,PhantomReference;按强度等级排列。

2. api接口:

Reference get 获取引用所指向的对象,若被回收返回空
ReferenceQueue poll 遍历队列,查看是否存在可用的引用对象,若存在,移除并返回,否则返回空
ReferenceQueue remove 移除队列中下一个引用对象

3. Reference区别

引用说明 特点 使用场景
Strong Reference(强引用) 只要对象存在引用,一直可以存活 正常使用比如:new
Soft Reference(软引用) 内存充足的时候,一直可以存活 缓存
Weak Reference(弱引用) 对象只要被使用,一直可以存活 在垃圾回收之后获取对象
Phantom Reference(幽灵引用) get返回一直为Null。 标识对象何时被回收
package com.pgy.collection;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

public class ReferenceDemo2 {
    private static ReferenceQueue<Object> objQueue = new ReferenceQueue<Object>();

    public static void main(String[] args) {
        Object obj = null;
        System.out.println("****************testSoftReference*******************");
        testSoftReference(obj);
        System.out.println("****************testWeakReference*******************");
        testWeakReference(obj);
        System.out.println("****************testPhantomReference*******************");
        testPhantomReference(obj);
    }

    private static void testSoftReference(Object obj) {
        obj = new Object();
        SoftReference<Object> sr = new SoftReference<Object>(obj, objQueue);

        System.out.println(sr.get());
        System.out.println(objQueue.poll());
        obj = null;
        System.gc();

        System.out.println(sr.get());
        System.out.println(objQueue.poll());
    }

    private static void testWeakReference(Object obj) {
        obj = new Object();
        WeakReference<Object> wr = new WeakReference<Object>(obj, objQueue);
        System.out.println(wr.get());
        System.out.println(objQueue.poll());
        obj = null;
        System.gc();

        System.out.println(wr.get());
        System.out.println(objQueue.poll());
    }

    private static void testPhantomReference(Object obj) {
        obj = new Object();
        PhantomReference<Object> pr = new PhantomReference<Object>(obj, objQueue);

        System.out.println(pr.get());
        System.out.println(objQueue.poll());
        obj = null;
        System.gc();

        System.out.println(pr.get());
        System.out.println(objQueue.poll());
    }

}
//console
//****************testSoftReference*******************
//java.lang.Object@280bca
//null
//java.lang.Object@280bca
//null
//****************testWeakReference*******************
//java.lang.Object@1817fe89
//null
//null
//java.lang.ref.WeakReference@717e5fde
//****************testPhantomReference*******************
//null
//null
//null
//java.lang.ref.PhantomReference@39fc0f04

4. 参考

  1. http://www.cnblogs.com/newcj/archive/2011/05/15/2046882.html
  2. http://mindprod.com/jgloss/phantom.html
  3. http://www.importnew.com/17019.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章