RFR: 8304818: Prune HttpURLConnection cache when corresponding Authenticator is garbage collected [v2]
Michael McMahon
michaelm at openjdk.org
Fri Mar 24 16:39:45 UTC 2023
On Thu, 23 Mar 2023 17:10:02 GMT, Michael McMahon <michaelm at openjdk.org> wrote:
>> 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.
Unfortunately, this is not sufficient. While the wait does indeed block until the reference has been cleared, the Cleaner is not guaranteed to have run at this point. So, I've had to include a sleep for 1 sec as well.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/13159#discussion_r1147818244
More information about the net-dev
mailing list