软引用 弱引用 虚引用 代码测试分析

代码如下:
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

public class A
{
    public static void main(String[] args)
    {
	System.out.println("begin");
	User user = new User("SoftReference");
	SoftReference<User> softRef = new SoftReference<User>(user);
	System.out.println("user SoftReference hase been GC : " + softRef.isEnqueued());
	user = null;
	System.out.println("user SoftReference hase been GC : " + softRef.isEnqueued());
	System.gc();
	System.out.println("user SoftReference hase been GC : " + softRef.isEnqueued());
	System.out.println("1 :" + (softRef.get() == null));
	System.out.println(softRef.get().getName());
	System.out.println();

	user = new User("WeakReference");
	WeakReference<User> weakRef = new WeakReference<User>(user);
	System.out.println("2 :" + (weakRef.get() == null));
	System.out.println("user WeakReference hase been GC : " + weakRef.isEnqueued());
	user = null;
	System.out.println("user WeakReference hase been GC : " + weakRef.isEnqueued());
	System.out.println("3 :" + (weakRef.get() == null));
	System.gc();
	System.out.println("user WeakReference hase been GC : " + weakRef.isEnqueued());
	System.out.println("4 :" + (weakRef.get() == null));
	System.out.println();

	user = new User("PhantomReference");
	ReferenceQueue<User> refQueue = new ReferenceQueue<User>();
	PhantomReference<User> phantomRef = new PhantomReference<User>(user, refQueue);
	System.out.println("5 :" + (phantomRef.get() == null));
	System.out.println("user PhantomReference hase been GC : " + phantomRef.isEnqueued());
	user = null;
	System.out.println("user PhantomReference hase been GC : " + phantomRef.isEnqueued());
	System.gc();
	System.out.println("user PhantomReference hase been GC : " + phantomRef.isEnqueued());
    }
}

class User
{
    private String name;

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

    public String getName()
    {
	return name;
    }
}



运行结果:
begin
user SoftReference hase been GC : false
user SoftReference hase been GC : false
user SoftReference hase been GC : false
1 :false
HELLO WORLD

2 :false
user WeakReference hase been GC : false
user WeakReference hase been GC : false
3 :false
user WeakReference hase been GC : false
4 :true

5 :true
user PhantomReference hase been GC : false
user PhantomReference hase been GC : false
user PhantomReference hase been GC : true

软引用分析:
一般用于缓存,只有当JVM内存不足时,才会被GC,所以 user=null,后仍没有被GC。只有把JVM的堆内存设置小些,比较有可能设置出GC效果。另外,当GC认为扫描到的SoftReference不能经常使用时,也进行回收,存活时间可通过: -XX:SoftRefLRUPPolicyMSPerMB 来进行控制,其含义是每MB堆空间中SofteReference的存活时间,默认为1秒。

弱引用:
只要GC,就会被回收。因为当前CPU性能比较高,一个CPU时间轮转周期,就能处理完代码,所以这里手动加了GC。不手动GC在这个时间单元内是不会被GC的,而且GC的场合还汲及  saftpoint。

虚引用 :
无论何时 get()都为空。但测试isEnqueued(), 他可以监控引用对象在内存中的状态,是否被GC。

你可能感兴趣的:(弱引用)