b6a0d1
diff --git a/java/org/apache/catalina/loader/WebappClassLoaderBase.java b/java/org/apache/catalina/loader/WebappClassLoaderBase.java
b6a0d1
index 8746b6b..dc878c6 100644
b6a0d1
--- a/java/org/apache/catalina/loader/WebappClassLoaderBase.java
b6a0d1
+++ b/java/org/apache/catalina/loader/WebappClassLoaderBase.java
b6a0d1
@@ -1820,41 +1820,13 @@ public abstract class WebappClassLoaderBase extends URLClassLoader
b6a0d1
                     // shutting down the executor
b6a0d1
                     boolean usingExecutor = false;
b6a0d1
                     try {
b6a0d1
-
b6a0d1
-                        // Runnable wrapped by Thread
b6a0d1
-                        // "target" in Sun/Oracle JDK
b6a0d1
-                        // "runnable" in IBM JDK
b6a0d1
-                        // "action" in Apache Harmony
b6a0d1
-                        Object target = null;
b6a0d1
-                        for (String fieldName : new String[] { "target", "runnable", "action" }) {
b6a0d1
-                            try {
b6a0d1
-                                Field targetField = thread.getClass().getDeclaredField(fieldName);
b6a0d1
-                                targetField.setAccessible(true);
b6a0d1
-                                target = targetField.get(thread);
b6a0d1
-                                break;
b6a0d1
-                            } catch (NoSuchFieldException nfe) {
b6a0d1
-                                continue;
b6a0d1
-                            }
b6a0d1
-                        }
b6a0d1
-
b6a0d1
-                        // "java.util.concurrent" code is in public domain,
b6a0d1
-                        // so all implementations are similar including our
b6a0d1
-                        // internal fork.
b6a0d1
-                        if (target != null && target.getClass().getCanonicalName() != null &&
b6a0d1
-                                (target.getClass().getCanonicalName().equals(
b6a0d1
-                                        "org.apache.tomcat.util.threads.ThreadPoolExecutor.Worker") ||
b6a0d1
-                                        target.getClass().getCanonicalName().equals(
b6a0d1
-                                                "java.util.concurrent.ThreadPoolExecutor.Worker"))) {
b6a0d1
-                            Field executorField = target.getClass().getDeclaredField("this$0");
b6a0d1
-                            executorField.setAccessible(true);
b6a0d1
-                            Object executor = executorField.get(target);
b6a0d1
-                            if (executor instanceof ThreadPoolExecutor) {
b6a0d1
-                                ((ThreadPoolExecutor) executor).shutdownNow();
b6a0d1
-                                usingExecutor = true;
b6a0d1
-                            } else if (executor instanceof java.util.concurrent.ThreadPoolExecutor) {
b6a0d1
-                                ((java.util.concurrent.ThreadPoolExecutor) executor).shutdownNow();
b6a0d1
-                                usingExecutor = true;
b6a0d1
-                            }
b6a0d1
+			Object executor = JreCompat.getInstance().getExecutor(thread);
b6a0d1
+                        if (executor instanceof ThreadPoolExecutor) {
b6a0d1
+                            ((ThreadPoolExecutor) executor).shutdownNow();
b6a0d1
+                            usingExecutor = true;
b6a0d1
+                        } else if (executor instanceof java.util.concurrent.ThreadPoolExecutor) {
b6a0d1
+                            ((java.util.concurrent.ThreadPoolExecutor) executor).shutdownNow();
b6a0d1
+                            usingExecutor = true;
b6a0d1
                         }
b6a0d1
                     } catch (NoSuchFieldException | IllegalAccessException | RuntimeException e) {
b6a0d1
                         // InaccessibleObjectException is only available in Java 9+,
b6a0d1
@@ -2306,6 +2278,12 @@ public abstract class WebappClassLoaderBase extends URLClassLoader
b6a0d1
 
b6a0d1
 
b6a0d1
     private void clearReferencesObjectStreamClassCaches() {
b6a0d1
+	if (JreCompat.isJre19Available()) {
b6a0d1
+            // The memory leak this fixes has been fixed in Java 19 onwards,
b6a0d1
+            // 17.0.4 onwards and 11.0.16 onwards
b6a0d1
+            // See https://bugs.openjdk.java.net/browse/JDK-8277072
b6a0d1
+            return;
b6a0d1
+        }
b6a0d1
         try {
b6a0d1
             Class clazz = Class.forName("java.io.ObjectStreamClass$Caches");
b6a0d1
             clearCache(clazz, "localDescs");
b6a0d1
@@ -2333,14 +2311,19 @@ public abstract class WebappClassLoaderBase extends URLClassLoader
b6a0d1
             throws ReflectiveOperationException, SecurityException, ClassCastException {
b6a0d1
         Field f = target.getDeclaredField(mapName);
b6a0d1
         f.setAccessible(true);
b6a0d1
-        Map map = (Map) f.get(null);
b6a0d1
-        Iterator keys = map.keySet().iterator();
b6a0d1
-        while (keys.hasNext()) {
b6a0d1
-            Object key = keys.next();
b6a0d1
-            if (key instanceof Reference) {
b6a0d1
-                Object clazz = ((Reference) key).get();
b6a0d1
-                if (loadedByThisOrChild(clazz)) {
b6a0d1
-                    keys.remove();
b6a0d1
+	Object map = f.get(null);
b6a0d1
+        // Avoid trying to clear references if Tomcat is running on a JRE that
b6a0d1
+        // includes the fix for this memory leak
b6a0d1
+        // See https://bugs.openjdk.java.net/browse/JDK-8277072
b6a0d1
+        if (map instanceof Map) {
b6a0d1
+            Iterator keys = ((Map) map).keySet().iterator();
b6a0d1
+            while (keys.hasNext()) {
b6a0d1
+                Object key = keys.next();
b6a0d1
+                if (key instanceof Reference) {
b6a0d1
+                    Object clazz = ((Reference) key).get();
b6a0d1
+                    if (loadedByThisOrChild(clazz)) {
b6a0d1
+                        keys.remove();
b6a0d1
+                    }
b6a0d1
                 }
b6a0d1
             }
b6a0d1
         }
b6a0d1
diff --git a/java/org/apache/tomcat/util/compat/JreCompat.java b/java/org/apache/tomcat/util/compat/JreCompat.java
b6a0d1
index 62df145..e5df728 100644
b6a0d1
--- a/java/org/apache/tomcat/util/compat/JreCompat.java
b6a0d1
+++ b/java/org/apache/tomcat/util/compat/JreCompat.java
b6a0d1
@@ -19,6 +19,7 @@ package org.apache.tomcat.util.compat;
b6a0d1
 import java.io.File;
b6a0d1
 import java.io.IOException;
b6a0d1
 import java.lang.reflect.AccessibleObject;
b6a0d1
+import java.lang.reflect.Field;
b6a0d1
 import java.lang.reflect.InvocationTargetException;
b6a0d1
 import java.lang.reflect.Method;
b6a0d1
 import java.net.SocketAddress;
b6a0d1
@@ -45,6 +46,7 @@ public class JreCompat {
b6a0d1
 
b6a0d1
     private static final JreCompat instance;
b6a0d1
     private static final boolean graalAvailable;
b6a0d1
+    private static final boolean jre19Available;
b6a0d1
     private static final boolean jre16Available;
b6a0d1
     private static final boolean jre11Available;
b6a0d1
     private static final boolean jre9Available;
b6a0d1
@@ -67,18 +69,26 @@ public class JreCompat {
b6a0d1
 
b6a0d1
         // This is Tomcat 9 with a minimum Java version of Java 8.
b6a0d1
         // Look for the highest supported JVM first
b6a0d1
-        if (Jre16Compat.isSupported()) {
b6a0d1
+	if (Jre19Compat.isSupported()) {
b6a0d1
+            instance = new Jre19Compat();
b6a0d1
+            jre9Available = true;
b6a0d1
+            jre16Available = true;
b6a0d1
+            jre19Available = true;
b6a0d1
+        } else if (Jre16Compat.isSupported()) {
b6a0d1
             instance = new Jre16Compat();
b6a0d1
             jre9Available = true;
b6a0d1
             jre16Available = true;
b6a0d1
+            jre19Available = false;
b6a0d1
         } else if (Jre9Compat.isSupported()) {
b6a0d1
             instance = new Jre9Compat();
b6a0d1
             jre9Available = true;
b6a0d1
             jre16Available = false;
b6a0d1
+            jre19Available = false;
b6a0d1
         } else {
b6a0d1
             instance = new JreCompat();
b6a0d1
             jre9Available = false;
b6a0d1
             jre16Available = false;
b6a0d1
+            jre19Available = false;
b6a0d1
         }
b6a0d1
         jre11Available = instance.jarFileRuntimeMajorVersion() >= 11;
b6a0d1
 
b6a0d1
@@ -124,6 +134,9 @@ public class JreCompat {
b6a0d1
         return jre16Available;
b6a0d1
     }
b6a0d1
 
b6a0d1
+    public static boolean isJre19Available() {
b6a0d1
+        return jre19Available;
b6a0d1
+    }
b6a0d1
 
b6a0d1
     // Java 8 implementation of Java 9 methods
b6a0d1
 
b6a0d1
@@ -303,6 +316,8 @@ public class JreCompat {
b6a0d1
     }
b6a0d1
 
b6a0d1
 
b6a0d1
+    // Java 8 implementations of Java 16 methods
b6a0d1
+
b6a0d1
     /**
b6a0d1
      * Return Unix domain socket address for given path.
b6a0d1
      * @param path The path
b6a0d1
@@ -329,4 +344,63 @@ public class JreCompat {
b6a0d1
     public SocketChannel openUnixDomainSocketChannel() {
b6a0d1
         throw new UnsupportedOperationException(sm.getString("jreCompat.noUnixDomainSocket"));
b6a0d1
     }
b6a0d1
+
b6a0d1
+
b6a0d1
+    // Java 8 implementations of Java 19 methods
b6a0d1
+
b6a0d1
+    /**
b6a0d1
+     * Obtains the executor, if any, used to create the provided thread.
b6a0d1
+     *
b6a0d1
+     * @param thread    The thread to examine
b6a0d1
+     *
b6a0d1
+     * @return  The executor, if any, that created the provided thread
b6a0d1
+     *
b6a0d1
+     * @throws NoSuchFieldException
b6a0d1
+     *              If a field used via reflection to obtain the executor cannot
b6a0d1
+     *              be found
b6a0d1
+     * @throws SecurityException
b6a0d1
+     *              If a security exception occurs while trying to identify the
b6a0d1
+     *              executor
b6a0d1
+     * @throws IllegalArgumentException
b6a0d1
+     *              If the instance object does not match the class of the field
b6a0d1
+     *              when obtaining a field value via reflection
b6a0d1
+     * @throws IllegalAccessException
b6a0d1
+     *              If a field is not accessible due to access restrictions
b6a0d1
+     */
b6a0d1
+    public Object getExecutor(Thread thread)
b6a0d1
+            throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
b6a0d1
+
b6a0d1
+        Object result = null;
b6a0d1
+
b6a0d1
+        // Runnable wrapped by Thread
b6a0d1
+        // "target" in Sun/Oracle JDK
b6a0d1
+        // "runnable" in IBM JDK
b6a0d1
+        // "action" in Apache Harmony
b6a0d1
+        Object target = null;
b6a0d1
+        for (String fieldName : new String[] { "target", "runnable", "action" }) {
b6a0d1
+            try {
b6a0d1
+                Field targetField = thread.getClass().getDeclaredField(fieldName);
b6a0d1
+                targetField.setAccessible(true);
b6a0d1
+                target = targetField.get(thread);
b6a0d1
+                break;
b6a0d1
+            } catch (NoSuchFieldException nfe) {
b6a0d1
+                continue;
b6a0d1
+            }
b6a0d1
+        }
b6a0d1
+
b6a0d1
+        // "java.util.concurrent" code is in public domain,
b6a0d1
+        // so all implementations are similar including our
b6a0d1
+        // internal fork.
b6a0d1
+        if (target != null && target.getClass().getCanonicalName() != null &&
b6a0d1
+                (target.getClass().getCanonicalName().equals(
b6a0d1
+                        "org.apache.tomcat.util.threads.ThreadPoolExecutor.Worker") ||
b6a0d1
+                        target.getClass().getCanonicalName().equals(
b6a0d1
+                                "java.util.concurrent.ThreadPoolExecutor.Worker"))) {
b6a0d1
+            Field executorField = target.getClass().getDeclaredField("this$0");
b6a0d1
+            executorField.setAccessible(true);
b6a0d1
+            result = executorField.get(target);
b6a0d1
+        }
b6a0d1
+
b6a0d1
+        return result;
b6a0d1
+    }
b6a0d1
 }
b6a0d1
diff --git a/java/org/apache/tomcat/util/compat/LocalStrings.properties b/java/org/apache/tomcat/util/compat/LocalStrings.properties
b6a0d1
index 79427da..c4c2f7d 100644
b6a0d1
--- a/java/org/apache/tomcat/util/compat/LocalStrings.properties
b6a0d1
+++ b/java/org/apache/tomcat/util/compat/LocalStrings.properties
b6a0d1
@@ -16,6 +16,8 @@
b6a0d1
 jre16Compat.javaPre16=Class not found so assuming code is running on a pre-Java 16 JVM
b6a0d1
 jre16Compat.unexpected=Failed to create references to Java 16 classes and methods
b6a0d1
 
b6a0d1
+jre19Compat.javaPre19=Class not found so assuming code is running on a pre-Java 19 JVM
b6a0d1
+
b6a0d1
 jre9Compat.invalidModuleUri=The module URI provided [{0}] could not be converted to a URL for the JarScanner to process
b6a0d1
 jre9Compat.javaPre9=Class not found so assuming code is running on a pre-Java 9 JVM
b6a0d1
 jre9Compat.unexpected=Failed to create references to Java 9 classes and methods
b6a0d1
diff --git a/webapps/docs/config/context.xml b/webapps/docs/config/context.xml
b6a0d1
index d118196..42dfe38 100644
b6a0d1
--- a/webapps/docs/config/context.xml
b6a0d1
+++ b/webapps/docs/config/context.xml
b6a0d1
@@ -769,7 +769,11 @@
b6a0d1
         therefore requires that the command line option
b6a0d1
         -XaddExports:java.base/java.io=ALL-UNNAMED is set
b6a0d1
         when running on Java 9 and above. If not specified, the default value of
b6a0d1
-        true will be used.

b6a0d1
+	true will be used.

b6a0d1
+	

The memory leak associated with ObjectStreamClass has

b6a0d1
+        been fixed in Java 19 onwards, Java 17.0.4 onwards and Java 11.0.16
b6a0d1
+        onwards. The check will be disabled when running on a version
b6a0d1
+        of Java that contains the fix.

b6a0d1
       </attribute>
b6a0d1
 
b6a0d1
       <attribute name="clearReferencesRmiTargets" required="false">
b6a0d1
diff --git a/java/org/apache/tomcat/util/compat/Jre19Compat.java b/java/org/apache/tomcat/util/compat/Jre19Compat.java
b6a0d1
new file mode 100644
b6a0d1
index 0000000000..fb94810b40
b6a0d1
--- /dev/null
b6a0d1
+++ b/java/org/apache/tomcat/util/compat/Jre19Compat.java
b6a0d1
@@ -0,0 +1,84 @@
b6a0d1
+/*
b6a0d1
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
b6a0d1
+ *  contributor license agreements.  See the NOTICE file distributed with
b6a0d1
+ *  this work for additional information regarding copyright ownership.
b6a0d1
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
b6a0d1
+ *  (the "License"); you may not use this file except in compliance with
b6a0d1
+ *  the License.  You may obtain a copy of the License at
b6a0d1
+ *
b6a0d1
+ *      http://www.apache.org/licenses/LICENSE-2.0
b6a0d1
+ *
b6a0d1
+ *  Unless required by applicable law or agreed to in writing, software
b6a0d1
+ *  distributed under the License is distributed on an "AS IS" BASIS,
b6a0d1
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
b6a0d1
+ *  See the License for the specific language governing permissions and
b6a0d1
+ *  limitations under the License.
b6a0d1
+ */
b6a0d1
+package org.apache.tomcat.util.compat;
b6a0d1
+
b6a0d1
+import java.lang.reflect.Field;
b6a0d1
+
b6a0d1
+import org.apache.juli.logging.Log;
b6a0d1
+import org.apache.juli.logging.LogFactory;
b6a0d1
+import org.apache.tomcat.util.res.StringManager;
b6a0d1
+
b6a0d1
+public class Jre19Compat extends Jre16Compat {
b6a0d1
+
b6a0d1
+    private static final Log log = LogFactory.getLog(Jre19Compat.class);
b6a0d1
+    private static final StringManager sm = StringManager.getManager(Jre19Compat.class);
b6a0d1
+
b6a0d1
+    private static final boolean supported;
b6a0d1
+
b6a0d1
+    static {
b6a0d1
+        // Don't need any Java 19 specific classes (yet) so just test for one of
b6a0d1
+        // the new ones for now.
b6a0d1
+        Class c1 = null;
b6a0d1
+        try {
b6a0d1
+            c1 = Class.forName("java.lang.WrongThreadException");
b6a0d1
+        } catch (ClassNotFoundException cnfe) {
b6a0d1
+            // Must be pre-Java 16
b6a0d1
+            log.debug(sm.getString("jre19Compat.javaPre19"), cnfe);
b6a0d1
+        }
b6a0d1
+
b6a0d1
+        supported = (c1 != null);
b6a0d1
+    }
b6a0d1
+
b6a0d1
+    static boolean isSupported() {
b6a0d1
+        return supported;
b6a0d1
+    }
b6a0d1
+
b6a0d1
+    @Override
b6a0d1
+    public Object getExecutor(Thread thread)
b6a0d1
+            throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
b6a0d1
+
b6a0d1
+        Object result = super.getExecutor(thread);
b6a0d1
+
b6a0d1
+        if (result == null) {
b6a0d1
+            Object holder = null;
b6a0d1
+            Object task = null;
b6a0d1
+            try {
b6a0d1
+                Field holderField = thread.getClass().getDeclaredField("holder");
b6a0d1
+                holderField.setAccessible(true);
b6a0d1
+                holder = holderField.get(thread);
b6a0d1
+
b6a0d1
+                Field taskField = holder.getClass().getDeclaredField("task");
b6a0d1
+                taskField.setAccessible(true);
b6a0d1
+                task = taskField.get(holder);
b6a0d1
+            } catch (NoSuchFieldException nfe) {
b6a0d1
+                return null;
b6a0d1
+            }
b6a0d1
+
b6a0d1
+            if (task!= null && task.getClass().getCanonicalName() != null &&
b6a0d1
+                    (task.getClass().getCanonicalName().equals(
b6a0d1
+                            "org.apache.tomcat.util.threads.ThreadPoolExecutor.Worker") ||
b6a0d1
+                            task.getClass().getCanonicalName().equals(
b6a0d1
+                                    "java.util.concurrent.ThreadPoolExecutor.Worker"))) {
b6a0d1
+                Field executorField = task.getClass().getDeclaredField("this$0");
b6a0d1
+                executorField.setAccessible(true);
b6a0d1
+                result = executorField.get(task);
b6a0d1
+            }
b6a0d1
+        }
b6a0d1
+
b6a0d1
+        return result;
b6a0d1
+    }
b6a0d1
+}