public class WeakReference<T> extends Reference<T> { public WeakReference(T referent) { super(referent); } public WeakReference(T referent, ReferenceQueue<? super T> q) { super(referent, q); } } public abstract class Reference<T> {…
Overview The java.lang.ref package provides more flexible types of references than are otherwise available, permitting limited interaction between the application and the Java Virtual Machine (JVM) garbage collector. It is an important package, centr…
在了解WeakReference之前,先给出一段简单的代码: public class WeakReferenceTest {public static void main(String[] args) throws Exception {Object o = new Object();// 默认的构造函数,会使用ReferenceQueue.NULL 作为queueWeakReference<Object> wr = new WeakReference<Object>(o);Sy…
在阅读Thinking in Java的Containers in depth一章中的Holding references时,提到了一个工具包java.lang.ref,说这是个为Java垃圾回收提供了很大的灵活性的包. 并引出了抽象类Reference还有它的三个子类,书上看了好几次都一脸懵逼……最后百度了很久现在简单记录总结一下. 一.为什么要有这个Reference类呢? 一般的对象在程序中,都是可获取的,也就是有直接引用的,或者用更专业的词(等等会介绍)就是强引用的对象.一般对于这样可以…