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

代码如下:

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。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值