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

Michael McMahon michaelm at openjdk.org
Thu Mar 23 17:12:37 UTC 2023


On Thu, 23 Mar 2023 16:38:47 GMT, Daniel Fuchs <dfuchs 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.wait(() -> ref.refersTo(null));

Good idea. The second approach seems quite tidy.

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

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


More information about the net-dev mailing list