RFR: 8304818: Prune HttpURLConnection cache when corresponding Authenticator is garbage collected

Daniel Fuchs dfuchs at openjdk.org
Thu Mar 23 16:47:07 UTC 2023


On Thu, 23 Mar 2023 15:47:10 GMT, Michael McMahon <michaelm at openjdk.org> wrote:

> Hi,
> 
> Can I get a review for this please? This change uses a Cleaner to check when java.net.Authenticator instances become unreachable, and then prunes the authentication cache for entries that correspond to that Authenticator and therefore cannot be used again. This stops the authentication cache from growing in size unbounded. Note, this only happens in the probably unusual case where more than one Authenticator object is used. Normally there is no reason to use more than one instance.
> 
> Thanks,
> Michael.

test/jdk/sun/net/www/protocol/http/AuthCache.java line 136:

> 134:         System.gc();
> 135:         delay(1);
> 136:         System.gc();

I suggest creating a weak reference (clauth1) with a reference queue, and then pull the reference queue in a loop (with a smaller sleep timeout), and calling System.gc() each time the timeout elapses.


        ReferenceQueue<Authenticator> queue = new ReferenceQueue();
        WeakReference<Authenticator> ref = new WeakReference<>(clauth1, queue);
        clauth1 = null;
        Reference<Authenticator> ref2 = null;
        while ((ref2 = queue.remove(100)) != ref) {
            System.gc();
        }


Alternatively you could try to use something like:


        WeakReference<Authenticator> ref = new WeakReference<>(clauth1);
        clauth1 = null;
        ForceGC.await(() -> ref.refersTo(null))

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

PR Review Comment: https://git.openjdk.org/jdk/pull/13159#discussion_r1146472798


More information about the net-dev mailing list