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

Roger Riggs rriggs at openjdk.java.net
Wed Dec 1 21:12:32 UTC 2021


On Wed, 1 Dec 2021 12:18:05 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 unused EntryFuture inner class from ObjectSteamClass

Without the use of SoftReference, memory pressure won't release any of the cached info.
That seems to swing the other way from overly aggressively freeing memory with the WeakReference (and needing to recompute) as the change originally proposed.  
Its hard to tell in what environments it might be observed.

src/java.base/share/classes/java/io/ObjectStreamClass.java line 2133:

> 2131:             if (oldReflector != null) {
> 2132:                 reflector = oldReflector;
> 2133:             }

Map.computeIfAbsent(key, () -> new FieldReflector(matchFields, localDesc));
might be more compact.

test/jdk/java/io/ObjectStreamClass/TestOSCClassLoaderLeak.java line 52:

> 50:         Class<?> loadClass = myOwnClassLoader.loadClass("ObjectStreamClass_MemoryLeakExample");
> 51:         Constructor con = loadClass.getConstructor();
> 52:         con.setAccessible(true);

Isn't the constructor already public?

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

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


More information about the core-libs-dev mailing list