为什么自从 java 9 PhantomReference java 文档声明它专用于 POST-mortem 清理操作,尽管它之前是 Pre-mortem

Why since java 9 PhantomReference java doc states that it is dedicated to the POST-mortem cleanup actions although it was PRE-mortem before

PhantomReference java doc for java 8 和 less 看起来像这样:

Phantom reference objects, which are enqueued after the collector determines that their referents may otherwise be reclaimed. Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism. If the garbage collector determines at a certain point in time that the referent of a phantom reference is phantom reachable, then at that time or at some later time it will enqueue the reference.

In order to ensure that a reclaimable object remains so, the referent of a phantom reference may not be retrieved: The get method of a phantom reference always returns null.

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

PhantomReference java doc for java 9 和更高看起来像这样:

Phantom reference objects, which are enqueued after the collector determines that their referents may otherwise be reclaimed. Phantom references are most often used to schedule post-mortem cleanup actions. Suppose the garbage collector determines at a certain point in time that an object is phantom reachable. At that time it will atomically clear all phantom references to that object and all phantom references to any other phantom-reachable objects from which that object is reachable. At the same time or at some later time it will enqueue those newly-cleared phantom references that are registered with reference queues.

In order to ensure that a reclaimable object remains so, the referent of a phantom reference may not be retrieved: The get method of a phantom reference always returns null.

PhantomReference 行为在 java 9 中有什么变化吗?或者只是 java 创始人重新考虑了 class 的奉献精神?

因为 Java 9,PhantomReference (PR) 是 automatically cleared。您看到的是 Javadoc 更改作为该更改的结果。

在 Java 9 之前,PR 引用的对象保持活动状态,即使它的 get() 会 return null。因此,在 PR 本身死亡之前,从技术上讲,指称对象仍然存在,尽管您无法获取对它的引用。这种行为的好处不是很明显。无论如何,PR 处理将是 "pre-mortem cleanup".

在Java 9之后,PR在入队之前被清除(就像其他类型的weak/soft refs),在PR被应用程序代码处理之前引用对象本身已经完全死亡,这将是"post-mortem cleanup".