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

拟墨画扇 提交于 2019-12-05 07:41:22

问题


PhantomReference java doc for java 8 and less looks like this:

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 and higher looks like this:

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.

Was something changing in PhantomReference behaviour in java 9? or just java founders rethought dedication of that class ?


回答1:


Since Java 9, PhantomReference (PR) are automatically cleared. What you see is the Javadoc change that comes as the result of that change.

Before Java 9, the object referenced by PR was kept alive, even though its get() would return null. Therefore, until PR itself is dead, the referent would be technically alive, although you could not acquire the reference to it. The benefits of this behavior are not very clear. Anyhow, PR handling would be the "pre-mortem cleanup".

After Java 9, PR is cleared right before enqueueing (just like other types of weak/soft refs), the referent itself becomes fully dead before PR is processed by application code, which would be the "post-mortem cleanup".



来源:https://stackoverflow.com/questions/56684150/why-since-java-9-phantomreference-java-doc-states-that-it-is-dedicated-to-the-po

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!