WeakReference和SoftReference都可以用来保存对象的实例引用,这两个类和JVM垃圾回收机制有关。
从名字上就可以看出WeakReference是一个弱引用,其中保存的对象可以被GC回收掉。这个引用在开发中不是经常使用,它主要应用在一些Debug,内存监视程序中,它的存在是不对程序产生影响的。
SoftReference是一个软引用,也是一个强的引用形式,它对程序是有影响的,他的影响方式是这样的,如果JVM内存已经装满,则回收这个引用中的空间。否则不回收。这样一个性质,我们可以用它来做Cache。
比如我们把这个类的对象结合HashMap来使用。在Hashmap里放入这个对象当Jvm没有满时系统访问的就是这个Map里的对象。当JVM满了的时候就会释放里面的对象,这样,如果我们需要显示的是网络上的内容则可以省去不断下载的麻烦。
下面是两个类的演示程序:
WeakReference:
import java.lang.ref.WeakReference;
public class WeakReferenceTest {
/**
* @param args
*/
public static void main(String[] args) {
A a = new A();
a.str = "Hello, reference";
WeakReference<A> weak = new WeakReference<A>(a);
a = null;
int i = 0;
while (weak.get() != null) {
System.out.println(String.format("Get str from object of WeakReference: %s, count: %d", weak.get().str, ++i));
if (i % 10 == 0) {
System.gc();
System.out.println("System.gc() was invoked!");
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
System.out.println("object a was cleared by JVM!");
}
}
运行结果:
Get str from object of WeakReference: Hello, reference, count: 1
Get str from object of WeakReference: Hello, reference, count: 2
Get str from object of WeakReference: Hello, reference, count: 3
Get str from object of WeakReference: Hello, reference, count: 4
Get str from object of WeakReference: Hello, reference, count: 5
Get str from object of WeakReference: Hello, reference, count: 6
Get str from object of WeakReference: Hello, reference, count: 7
Get str from object of WeakReference: Hello, reference, count: 8
Get str from object of WeakReference: Hello, reference, count: 9
Get str from object of WeakReference: Hello, reference, count: 10
System.gc() was invoked!
object a was cleared by JVM!
这里可以看出它只运行了一次。
SoftReference:
import java.lang.ref.SoftReference;
public class SoftReferenceTest {
/**
* @param args
*/
public static void main(String[] args) {
A a = new A();
a.str = "Hello, reference";
SoftReference<A> sr = new SoftReference<A>(a);
a = null;
int i = 0;
while (sr.get() != null) {
System.out.println(String.format("Get str from object of SoftReference: %s, count: %d", sr.get().str, ++i));
if (i % 10 == 0) {
System.gc();
System.out.println("System.gc() was invoked!");
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
System.out.println("object a was cleared by JVM!");
}
}
运行结果:
Get str from object of SoftReference: Hello, reference, count: 1
Get str from object of SoftReference: Hello, reference, count: 2
Get str from object of SoftReference: Hello, reference, count: 3
Get str from object of SoftReference: Hello, reference, count: 4
Get str from object of SoftReference: Hello, reference, count: 5
Get str from object of SoftReference: Hello, reference, count: 6
Get str from object of SoftReference: Hello, reference, count: 7
Get str from object of SoftReference: Hello, reference, count: 8
Get str from object of SoftReference: Hello, reference, count: 9
Get str from object of SoftReference: Hello, reference, count: 10
System.gc() was invoked!
Get str from object of SoftReference: Hello, reference, count: 11
Get str from object of SoftReference: Hello, reference, count: 12
Get str from object of SoftReference: Hello, reference, count: 13
Get str from object of SoftReference: Hello, reference, count: 14
Get str from object of SoftReference: Hello, reference, count: 15
Get str from object of SoftReference: Hello, reference, count: 16
Get str from object of SoftReference: Hello, reference, count: 17
Get str from object of SoftReference: Hello, reference, count: 18
Get str from object of SoftReference: Hello, reference, count: 19
Get str from object of SoftReference: Hello, reference, count: 20
System.gc() was invoked!
Get str from object of SoftReference: Hello, reference, count: 21
Get str from object of SoftReference: Hello, reference, count: 22
Get str from object of SoftReference: Hello, reference, count: 23
Get str from object of SoftReference: Hello, reference, count: 24
Get str from object of SoftReference: Hello, reference, count: 25
Get str from object of SoftReference: Hello, reference, count: 26
Get str from object of SoftReference: Hello, reference, count: 27
Get str from object of SoftReference: Hello, reference, count: 28
这里可以看出只要JVM分配的内存没有满就会一直运行下去