`

WeakReference

阅读更多

reachable, the following happens:
A set ref of references is determined. ref contains the following elements:
All weak references pointing to obj.
All weak references pointing to objects from which obj is either strongly or softly reachable.
All references in ref are atomically cleared.
All objects formerly being referenced by ref become eligible for finalization.
At some future point, all references in ref will be enqueued with their corresponding reference queues, if any.
Weak references are useful for mappings that should have their entries removed automatically once they are not referenced any more (from outside). The difference between a SoftReference and a WeakReference is the point of time at which the decision is made to clear and enqueue the reference:
A SoftReference should be cleared and enqueued as late as possible, that is, in case the VM is in danger of running out of memory.
A WeakReference may be cleared and enqueued as soon as is known to be weakly-referenced.

(1)WeakReference
   WeakReference实现了一个弱引用,弱引用是三种引用(StrongReference、WeakReference、SoftReference)中的一个。一旦垃圾回收器判定一个对象是是弱获取(对象可获取程度分为五种strongly reachable,softly reachable、weakly reachable、phantomly reachable、unreachable),则下列情况发生:
计算一组引用的ref,ref包括下列元素:
所有指向obj的弱引用
所有指向objects的弱引用,objects是软获取对象或者是强获取对象
在ref中的所有引用被自动删除
所有以前被ref引用的对象都符合终止条件的对象(become eligible for finalization)
在未来的某个时间,所有的在ref中的引用将被放入合适的引用队列中
弱引用对那些映射,这些映射中的实体的引用一旦被不存在这些实体将被自动删除。弱引用和软引用的区别是清空和将加入排队的时间点不同:
一个弱引用应该尽可能晚的被清除和加入队列,那是因为如果内存不足是vm将是危险的
弱引用对象是一旦知道引用的是弱获取对象就会被清除和入队。

 

A SoftReference should be cleared and enqueued as late as possible, that is, in case the VM is in danger of running out of memory.

一个软引用应该尽可能迟的被清理和入列,因此,在这种情况下虚拟机(VM)就存在内存溢出的风险(危险)。

个人理解:问题在“as late as possible”——始终过迟的清理垃圾,肯定就有内存溢出的风险(危险)。 
------------------------------------
All objects formerly being referenced by ref become eligible for finalization.
所有先前被ref引用的对象都将符合垃圾回收(finalization)的条件。
ref:地址引用,java里就是指被new创建的对象。
除非是使用JNI,所占用的内存将不在ref的范围(Thinking in Java 4th, 120页)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics