You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2022/05/18 18:53:10 UTC

[tomcat] branch 10.0.x updated: The ObjectStreamClass memory leak has been fixed in newer JREs

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 10.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/10.0.x by this push:
     new 06176a6d90 The ObjectStreamClass memory leak has been fixed in newer JREs
06176a6d90 is described below

commit 06176a6d9016991157af89eb90e4e8b516a6221d
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 fb29d6bdeb..40a6dd9c42 100644
--- a/java/org/apache/catalina/loader/WebappClassLoaderBase.java
+++ b/java/org/apache/catalina/loader/WebappClassLoaderBase.java
@@ -2316,6 +2316,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");
@@ -2343,14 +2349,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 5a519acaa2..7996901586 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 3360cec8bf..80309e8010 100644
--- a/webapps/docs/config/context.xml
+++ b/webapps/docs/config/context.xml
@@ -800,6 +800,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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org