On Thu, 30 Jun 2022 20:36:35 GMT, Roger Riggs <[email protected]> wrote:
>> I'm not sure if all unused object will be collected in one GC call always.
>> The gc() specification says "When control returns from the method call, the
>> Java Virtual Machine has made a best effort to reclaim space from all unused
>> objects. ... There is also no guarantee that this effort will determine the
>> change of reachability in any particular number of objects, or that any
>> particular number of {@link java.lang.ref.Reference Reference} objects will
>> be cleared and enqueued." But from the spec, I did not get a clear answer
>> for the question.
>>
>> If the `booleanSupplier` object could be cleared in a collection other than
>> the `ref` collection, the current code may be safer.
>
> True, knowing when GC is 'done' is not deterministic except for a specify
> Reference to a specific object.
> System.gc is just a request, the checking for an object can more quickly exit
> the loop.
> The code is as is, and already commented, might wait an extra cycle, but only
> in the case of a race between the requested predicate and the object being
> reclaimed. Ok as it.
Maybe not for this PR - but it could be useful to have a version of ForceGC
that takes as parameter a ReferenceQueue<T> and a Reference<T>. That would be
more efficient than creating a new object and waiting for a different cleaner
thread to cleanup that object.
-------------
PR: https://git.openjdk.org/jdk/pull/8979