从 JDK1.2 版本开始,把对象的引用分为四种级别,从而使程序能更加灵活的控制对象的生命周期。这四种级别由高到低依次为:强引用、软引用、弱引用和虚引用。
强引用(StrongReference)
我们使用的大部分引用实际上都是强引用,这是使用最普遍的引用。如果一个对象具有强引用,那就类似于必不可少的生活用品,垃圾回收器绝不会回收它。当内存空间不足,Java 虚拟机宁愿抛出OutOfMemoryError 错误,使程序异常终止,也不会靠随意回收具有强引用的对象来解决内存不足问题。
软引用(SoftReference)
如果内存空间足够,垃圾回收器就不会回收它,如果内存空间不足了,就会回收这些对象的内存。只要垃圾回收器没有回收它,该对象就可以被程序使用。
弱引用(WeakReference)
在垃圾回收器线程扫描它 所管辖的内存区域的过程中,一旦发现了只具有弱引用的对象,不管当前内存空间足够与否,都会回收它的内存。不过,由于垃圾回收器是一个优先级很低的线程, 因此不一定会很快发现那些只具有弱引用的对象。 弱引用可以和一个引用队列(ReferenceQueue)联合使用,如果弱引用所引用的对象被垃圾回收,Java 虚拟机就会把这个弱引用加入到与之关联的引用队列中。
虚引用(PhantomReference)
如果一个对象仅持有虚引用,那么它就和没有任何引用一样,在任何时候都可能被垃圾回收。虚引用主要用来跟踪对象被垃圾回收的活动。虚引用与软引用和弱引用的一个区别在于:虚引用必须和引用队列(ReferenceQueue)联合使用。当垃圾回收器准备回收一个对象时,如果发现它还有虚引用,就会在回收对象的内存之前,把这个虚引用加入到与之关联的引用队列中。程序可以通过判断引用队列中是否已经加入了虚引用,来了解被引用的对象是否将要被垃圾回收。程序如果发现某个虚引用已经被加入到引用队列,那么就可以在所引用的对象的内存被回收之前采取必要的行动。
下面看两个 Demo
public class Demo1 {
public static void main(String[] args) {
//这就是一个强引用
String str="hello";
//现在我们由上面的强引用创建一个软引用,所以现在 str 有两个引用指向它
SoftReference<String> soft=new SoftReference<String>(str);
str=null;
//可以使用 get()得到引用指向的对象
System.out.println(soft.get());//输出 hello
}
}
public class Demo2 {
public static void main(String[] args) {
//这就是一个强引用
String str="hello";
ReferenceQueue<? super String> q=new ReferenceQueue<String>();
//现在我们由上面的强引用创建一个虚引用,所以现在 str 有两个引用指向它
PhantomReference<String> p=new PhantomReference<String>(str, q);
//可以使用 get()得到引用指向的对象
System.out.println(q.poll());//输出 null
}
下面再看一个,首先创建一个 Store 类,内部定义一个很大的数组,目的是创建对象时,会得到更多的内存,以提高回收的可能性!
public class Store {
public static final int SIZE = 10000;
private double[] arr = new double[SIZE];
private String id;
public Store() {
}
public Store(String id) {
super();
this.id = id;
}
@Override
protected void finalize() throws Throwable {
System.out.println(id + "被回收了");
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return id;
}
}
依次创建软引用,弱引用,虚引用个 10 个!
public class Demo3 {
public static ReferenceQueue<Store> queue = new ReferenceQueue<Store>();
public static void checkQueue()
{
if(queue!=null)
{
@SuppressWarnings("unchecked")
Reference<Store> ref =(Reference<Store>)queue.poll();
if(ref!=null)
System.out.println(ref+"......"+ref.get());
}
}
public static void main(String[] args) {
HashSet<SoftReference<Store>> hs1 = new HashSet<SoftReference<Store>>();
HashSet<WeakReference<Store>> hs2 = new HashSet<WeakReference<Store>>();
//创建 10 个软引用
for(int i=1;i<=10;i++)
{
SoftReference<Store> soft = new SoftReference<Store>(new Store("soft"+i),queue);
System.out.println("create soft"+soft.get());
hs1.add(soft);
}
System.gc();
checkQueue();
//创建 10 个弱引用
for(int i=1;i<=10;i++)
{
WeakReference<Store> weak = new WeakReference<Store>(new Store("weak"+i),queue);
System.out.println("create weak"+weak.get());
hs2.add(weak);
}
System.gc();
checkQueue();
//创建 10 个虚引用
HashSet<PhantomReference<Store>> hs3 = new HashSet<PhantomReference<Store>>();
for(int i=1;i<=10;i++)
{
PhantomReference<Store> phantom = new PhantomReference<Store>(new Store("phantom"+i),queue);
System.out.println("create phantom "+phantom.get());
hs3.add(phantom);
}
System.gc();
checkQueue();
}
}
程序执行结果:

可以看到虚引用和弱引用被回收掉。。。
网友评论