RFR: 8339687: Rearrange reachabilityFence()s in jdk.test.lib.util.ForceGC [v2]

Brent Christian bchristi at openjdk.org
Tue Sep 10 20:14:07 UTC 2024


On Tue, 10 Sep 2024 19:23:16 GMT, Brent Christian <bchristi at openjdk.org> wrote:

>> From the bug description:
>> ForceGC would be improved by moving the Reference.reachabilityFence() calls for 'obj' and 'ref'.
>> 
>> Reference.reachabilityFence(obj) is currently placed after 'obj' has been set to null, so effectively does nothing. It should occur before obj = null;
>> 
>> For Reference.reachabilityFence(ref): 'ref' is a PhantomReference to 'obj', and is registered with 'queue'. ForceGC.waitFor() later remove()s the reference from the queue, as an indication that some GC and reference processing has taken place (hopefully causing the BooleanSupplier to return true).
>> 
>> The code expects the PhantomReference to be cleared and be put on the queue. But recall that a Reference refers to its queue, and not the other way around. If a Reference becomes unreachable and is garbage collected, it will never be enqueued.
>> 
>> I argue that the VM/GC could determine that 'ref' is not used by waitFor() and collect it before the call to queue.remove(). Moving Reference.reachabilityFence(ref) after the for() loop would prevent this scenario.
>> 
>> While this is only a very minor deficiency in ForceGC, I believe it would be good to ensure that the code behaves as expected.
>
> Brent Christian has updated the pull request incrementally with one additional commit since the last revision:
> 
>   add try-finally block

I've added a try/finally block for `ref`. I've also kept the `obj = null` assignment.

This test library is used in many places, to confirm behavior reliant on GC action. IMO it's best to code it in a way that's sure to behave as expected, and to keep it clear what it's meant to do.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/20898#issuecomment-2341922732


More information about the core-libs-dev mailing list