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 ?
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