为什么幻影引用在入队时没有被清除?
我们可以看到幻影可达";与无法访问"一样无法访问:§
We can see that "phantom reachable" is as unreachable as "unreachable": §
一个对象是幻象可达的,如果它既不是强、软或弱可达的,它已经被终结,并且一些幻象引用引用了它.
An object is phantom reachable if it is neither strongly, softly, nor weakly reachable, it has been finalized, and some phantom reference refers to it.
最后,一个对象是无法访问的,因此当它无法通过上述任何一种方式访问时,它就有资格进行回收.
Finally, an object is unreachable, and therefore eligible for reclamation, when it is not reachable in any of the above ways.
现在,来自:http://download.oracle.com/javase/6/docs/api/java/lang/ref/PhantomReference.html
与软引用和弱引用不同,幻像引用不会被垃圾收集器自动清除,因为它们被排入队列.可以通过幻像引用访问的对象将保持不变,直到所有此类引用都被清除或自身变得不可访问.
Unlike soft and weak references, phantom references are not automatically cleared by the garbage collector as they are enqueued. An object that is reachable via phantom references will remain so until all such references are cleared or themselves become unreachable.
基本原理是什么?有没有?
What's the underlying rationale? Is there even one?
这又是一个典型的 Java API 怪癖案例吗?
Is this yet another typical case of Java API quirk?
推荐答案
入队时清除软引用,因为软引用的主要用途是允许缓存大对象,清除软引用允许缓存的大对象垃圾收集.
Soft references are cleared when enqueued because the primary use of soft references are to allow caching of large objects, and clearing the soft references allows the large cached object to be garbage collected.
弱引用在入队时会被清除,因为弱引用的主要用途是允许一个对象引用一个对象,而不会阻止它被垃圾回收,因此在对象入队后立即清除引用可以让对象被垃圾回收.
Weak references are cleared when enqueued because the primary use of weak references are to allow one to reference an object without preventing it from being garbage collected, so clearing the references as soon as the object is enqueued allows the object to be garbage collected.
在入队时不会清除幻像引用,因为幻像引用的一个用例是允许在对象被垃圾回收之前执行清理.通过不清除引用,在用户清除对该对象的 PhantomReference 或 PhantomReference 本身已被垃圾回收之前,该对象仍保持幻像可访问(并且不符合垃圾回收条件).
Phantom references are not cleared when enqueued since one use case of phantom references is to allow performing cleanup before an object is garbage collected. By not clearing the references, the object remains phantomly reachable (and not eligible for garbage collected) until after the PhantomReference to that object is cleared by the user, or the PhantomReference is itself garbage collected.
这在 这里进行了解释,
如果一个对象既不是强、软或弱可达的,它已经被最终确定,并且一些幻像引用引用它,则它是幻像可达的.
An object is phantom reachable if it is neither strongly, softly, nor weakly reachable, it has been finalized, and some phantom reference refers to it.
最后,当一个对象无法通过上述任何一种方式到达时,它是不可达的,因此有资格进行回收.
Finally, an object is unreachable, and therefore eligible for reclamation, when it is not reachable in any of the above ways.
相关文章