JDK-8199589 : ObjectStreamClass does not provide a mechanism to clear the cache
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io:serialization
  • Affected Version: 8,9,10,11
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86_64
  • Submitted: 2018-03-13
  • Updated: 2022-05-18
  • Resolved: 2022-05-18
Related Reports
Duplicate :  
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
It is not possible (without using reflection) to clear the references held in ObjectStreamClass$Caches. Such a use of reflection is strongly discouraged in Java 9 onwards.

A method is requested that removes all classes from the caches that have been loaded by the given class loader or a descendant.

JUSTIFICATION :
One of the features of Apache Tomcat is detection of memory leaks in web applications. For this detection to work, it is necessary that all valid/expected references to a web application class loader can be removed. A combination of a WeakReference to the web application class loader and an explicit GC call then enables Tomcat to determine if any unexpected references have been retained to the web application class loader which would indicate a memory leak.

The references retained in ObjectStreamClass$Caches trigger a false positive in Tomcat's memory leak detection. It is necessary to clear those references for the detection to work correctly. An API to do this is requested.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Something along the lines of this API:

ObjectStreamClass.clearCaches(ClassLoader classLoader)

that then did the equivalent of cleanUpLeakingObjects() in https://github.com/markt-asf/memory-
leaks/blob/master/src/org/apache/markt/leaks/io/ObjectStreamClassLeak.java

ACTUAL -
The clean-up has to be performed via reflection.

---------- BEGIN SOURCE ----------
This class demonstrates the leak:

https://github.com/markt-asf/memory-
leaks/blob/master/src/org/apache/markt/leaks/io/ObjectStreamClassLeak.java

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
See the code in cleanUpLeakingObjects()


Comments
Tested with Apache Tomcat and confirmed this is a duplicate and that the issue is fixed with the latest Java 19 EA build.
18-05-2022