RFR: 8287384: Speed up jdk.test.lib.util.ForceGC

Roger Riggs rriggs at openjdk.java.net
Thu May 26 21:25:40 UTC 2022


On Thu, 26 May 2022 18:50:07 GMT, Xue-Lei Andrew Fan <xuelei at openjdk.org> wrote:

> Hi,
> 
> May I have this test update reviewed?
> 
> The ForceGC could be enhanced by using smaller wait/sleep time, and shared cleaner.
> 
> Thanks,
> Xuelei

Even using a Cleaner is a more overhead than necessary.
I would have skipped the overhead of a cleaner and Atomic classes with something more self contained as a static method:


   /**
     * The garbage collector is invoked to find unreferenced objects.
     * A new object is created and then freed. The method returns
     * when the object has been reclaimed or the GC did not complete in 10 seconds.
     *
     * @return true if GC was complete, false if did not complete after 10 Seconds
     */
    public static boolean waitForGC() {
        ReferenceQueue<Object> queue = new ReferenceQueue<>();
        Object obj = new Object();
        PhantomReference<Object> ref = new PhantomReference<>(obj, queue);
        obj = null;
        Reference.reachabilityFence(obj);
        Reference.reachabilityFence(ref);
        System.gc();

        for (int retries = 100; retries > 0; retries--) {
            try {
                var r = queue.remove(100L);
                if (r != null) {
                    return true;
                }
            } catch (InterruptedException ie) {
                // ignore, the loop will try again
            }
            System.gc();
        }
        return false;
    }

YMMV

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

PR: https://git.openjdk.java.net/jdk/pull/8907


More information about the core-libs-dev mailing list