RFR: 8277072: ObjectStreamClass caches keep ClassLoaders alive [v6]

Peter Levart plevart at openjdk.java.net
Sat Dec 4 08:53:13 UTC 2021


On Thu, 2 Dec 2021 16:22:02 GMT, Roman Kennke <rkennke at openjdk.org> wrote:

>> The caches in ObjectStreamClass basically map WeakReference<Class> to SoftReference<ObjectStreamClass>, where the ObjectStreamClass also references the same Class. That means that the cache entry, and thus the class and its class-loader, will not get reclaimed, unless the GC determines that memory pressure is very high.
>> 
>> However, this seems bogus, because that unnecessarily keeps ClassLoaders and all its classes alive much longer than necessary: as soon as a ClassLoader (and all its classes) become unreachable, there is no point in retaining the stuff in OSC's caches.
>> 
>> The proposed change is to use WeakReference instead of SoftReference for the values in caches.
>> 
>> Testing:
>>  - [x] tier1
>>  - [x] tier2
>>  - [x] tier3
>>  - [ ] tier4
>
> Roman Kennke has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Remove unnecessary import

Changes requested by plevart (Reviewer).

src/java.base/share/classes/java/io/ClassCache.java line 63:

> 61:             protected SoftReference<T> computeValue(Class<?> type) {
> 62:                 return new SoftReference<>(ClassCache.this.computeValue(type), queue);
> 63:             }

How does this work? You create a bare SoftReference here and register it with queue....

...then down in processQueue() you pick it up and cast to CacheRef... Doesn't this throw ClassCastException ?

src/java.base/share/classes/java/io/ClassCache.java line 85:

> 83:             CacheRef<? extends T> cacheRef = (CacheRef<? extends T>)ref;
> 84:             map.remove(cacheRef.getType());
> 85:         }

See the cast to CacheRef here?

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

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


More information about the core-libs-dev mailing list