有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么虚拟引用在排队时没有被清除?

我们可以看到,“幻影可达”与“不可达”一样不可达:§

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.

基本原理是什么?甚至有一个吗

这是JavaAPI怪癖的另一个典型例子吗


共 (2) 个答案

  1. # 1 楼答案

    软引用在排队时被清除,因为软引用的主要用途是允许缓存大型对象,而清除软引用则允许对大型缓存对象进行垃圾收集

    弱引用在排队时会被清除,因为弱引用的主要用途是允许引用对象,而不阻止对象被垃圾收集,因此在对象排队后立即清除引用将允许对象被垃圾收集

    排队时不会清除幻象引用,因为幻象引用的一个用例是允许在垃圾收集对象之前执行清理。通过不清除引用,对象将保持幻象可访问性(并且不符合垃圾收集的条件),直到用户清除对该对象的幻象引用,或者幻象引用本身被垃圾收集

    这是解释here

    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.

  2. # 2 楼答案

    这是jdk9中的was changed。现在,幻影引用会像软引用和弱引用一样被清除。相应的段落从Javadoc中删除

    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.