This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 8.5.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/8.5.x by this push: new 7cdce2a6ea The ObjectStreamClass memory leak has been fixed in newer JREs 7cdce2a6ea is described below commit 7cdce2a6ea2b8dbd39c7442bc5262b864dfd596f Author: Mark Thomas <ma...@apache.org> AuthorDate: Wed May 18 19:52:00 2022 +0100 The ObjectStreamClass memory leak has been fixed in newer JREs https://bugs.openjdk.java.net/browse/JDK-8277072 Disable the clean-up code when running on a JRE that includes the fix. --- .../catalina/loader/WebappClassLoaderBase.java | 27 +++++++++++++++------- webapps/docs/changelog.xml | 5 ++++ webapps/docs/config/context.xml | 4 ++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/java/org/apache/catalina/loader/WebappClassLoaderBase.java b/java/org/apache/catalina/loader/WebappClassLoaderBase.java index 8b0ce5ca08..55617624ce 100644 --- a/java/org/apache/catalina/loader/WebappClassLoaderBase.java +++ b/java/org/apache/catalina/loader/WebappClassLoaderBase.java @@ -2284,6 +2284,12 @@ public abstract class WebappClassLoaderBase extends URLClassLoader private void clearReferencesObjectStreamClassCaches() { + if (JreCompat.isJre19Available()) { + // The memory leak this fixes has been fixed in Java 19 onwards, + // 17.0.4 onwards and 11.0.16 onwards + // See https://bugs.openjdk.java.net/browse/JDK-8277072 + return; + } try { Class<?> clazz = Class.forName("java.io.ObjectStreamClass$Caches"); clearCache(clazz, "localDescs"); @@ -2311,14 +2317,19 @@ public abstract class WebappClassLoaderBase extends URLClassLoader throws ReflectiveOperationException, SecurityException, ClassCastException { Field f = target.getDeclaredField(mapName); f.setAccessible(true); - Map<?,?> map = (Map<?,?>) f.get(null); - Iterator<?> keys = map.keySet().iterator(); - while (keys.hasNext()) { - Object key = keys.next(); - if (key instanceof Reference) { - Object clazz = ((Reference<?>) key).get(); - if (loadedByThisOrChild(clazz)) { - keys.remove(); + Object map = f.get(null); + // Avoid trying to clear references if Tomcat is running on a JRE that + // includes the fix for this memory leak + // See https://bugs.openjdk.java.net/browse/JDK-8277072 + if (map instanceof Map<?,?>) { + Iterator<?> keys = ((Map<?,?>) map).keySet().iterator(); + while (keys.hasNext()) { + Object key = keys.next(); + if (key instanceof Reference) { + Object clazz = ((Reference<?>) key).get(); + if (loadedByThisOrChild(clazz)) { + keys.remove(); + } } } } diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 91d7bc229b..2878a25eea 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -115,6 +115,11 @@ Improve the error message if a required <code>--add-opens</code> option is missing. (markt) </fix> + <fix> + Disable the memory leak correction code enabled by the Context attribute + <code>clearReferencesObjectStreamClassCaches</code> when running on a + JRE that includes a fix for the underlying memory leak. (markt) + </fix> </changelog> </subsection> <subsection name="Jasper"> diff --git a/webapps/docs/config/context.xml b/webapps/docs/config/context.xml index 7177d3858c..5129e7bbc0 100644 --- a/webapps/docs/config/context.xml +++ b/webapps/docs/config/context.xml @@ -753,6 +753,10 @@ <code>-XaddExports:java.base/java.io=ALL-UNNAMED</code> is set when running on Java 9 and above. If not specified, the default value of <code>true</code> will be used.</p> + <p>The memory leak associated with <code>ObjectStreamClass</code> has + been fixed in Java 19 onwards, Java 17.0.4 onwards and Java 11.0.16 + onwards. The check will be disabled when running on a version + of Java that contains the fix.</p> </attribute> <attribute name="clearReferencesRmiTargets" required="false"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org