在论坛里面看到了帖子:http://www.iteye.com/topic/401478,下面是我对四种引用的试用:
1、强引用:
/** 强引用,JVM的默认实现 */
public static void test1() throws InterruptedException {
Object obj = new Object();
Object strong = obj;
obj = null;
System.gc();
Thread.sleep(1000);
System.out.println(strong);
}
输出如下:
java.lang.Object@35ce36
2、弱引用:
/**
* WeakReference 弱引用( 当所引用的对象在 JVM 内不再有强引用时, GC 后weak reference 将会被自动回收)
* */
public static void test2() throws InterruptedException {
Object obj = new Object();
WeakReference<Object> wr = new WeakReference<Object>(obj);
obj = null;
System.gc();
Thread.sleep(1000);
System.out.println(wr.get());
}
输出如下:
null
3、软引用:
/**
* SoftReference SoftReference 于 WeakReference 的特性基本一致, 最大的区别在于
* SoftReference 会尽可能长的保留引用直到 JVM 内存不足时才会被回收(虚拟机保证)
* */
public static void test3() throws InterruptedException {
Object obj = new Object();
SoftReference<Object> sr = new SoftReference<Object>(obj);
obj = null;
System.gc();
Thread.sleep(1000);
System.out.println(sr.get());
}
输出如下:
java.lang.Object@35ce36
4、幽灵引用:
/**
* PhantomReference Phantom Reference(幽灵引用) 与 WeakReference 和 SoftReference
* 有很大的不同, 因为它的 get() 方法永远返回 null
* */
public static void test4() throws InterruptedException {
Object obj = new Object();
ReferenceQueue<Object> rq = new ReferenceQueue<Object>();
PhantomReference<Object> pr = new PhantomReference<Object>(obj, rq);
System.out.println(pr.get());
}
输出如下:
null
5、ReferenceQueue:
public static void test5() throws InterruptedException {
Object obj = new Object();
ReferenceQueue<Object> rq = new ReferenceQueue<Object>();
WeakReference<Object> pr = new WeakReference<Object>(obj, rq);
System.out.println(pr.isEnqueued());
System.out.println(rq.poll());
obj = null;
System.gc();
System.out.println(pr.isEnqueued());
System.out.println(rq.remove().get());
}
输出如下:
false
null
true
null
6、WeakHashMap:
/**
* 使用 WeakReference 作为 key, 一旦没有指向 key 的强引用,
* WeakHashMap 在 GC 后将自动删除相关的
* entry
*/
public static void test6() throws InterruptedException {
Map<Object, Object> map = new WeakHashMap<Object, Object>();
Object key = new Object();
Object value = new Object();
map.put(key, value);
key = null;
System.gc();
Thread.sleep(1000);
System.out.println(value);
System.out.println(map.containsValue(value));
}
输出如下:
java.lang.Object@757aef
false