From bfc981d769ae00bf0ba37d36e131758fd475315a Mon Sep 17 00:00:00 2001
From: markt false will be used.
*/
private boolean clearReferencesStopThreads = false;
-
+
+ /**
+ * Should Tomcat attempt to clear any ThreadLocal objects that are instances
+ * of classes loaded by this class loader. Failure to remove any such
+ * objects will result in a memory leak on web application stop, undeploy or
+ * reload. It is disabled by default since the clearing of the ThreadLocal
+ * objects is not performed in a thread-safe manner.
+ */
+ private boolean clearReferencesThreadLocals = false;
+
/**
* Should the effective web.xml be logged when the context starts?
*/
@@ -2288,6 +2297,34 @@ public class StandardContext
/**
+ * Return the clearReferencesThreadLocals flag for this Context.
+ */
+ public boolean getClearReferencesThreadLocals() {
+
+ return (this.clearReferencesThreadLocals);
+
+ }
+
+
+ /**
+ * Set the clearReferencesStopThreads feature for this Context.
+ *
+ * @param clearReferencesStopThreads The new flag value
+ */
+ public void setClearReferencesThreadLocals(
+ boolean clearReferencesThreadLocals) {
+
+ boolean oldClearReferencesThreadLocals =
+ this.clearReferencesThreadLocals;
+ this.clearReferencesThreadLocals = clearReferencesThreadLocals;
+ support.firePropertyChange("clearReferencesStopThreads",
+ oldClearReferencesThreadLocals,
+ this.clearReferencesThreadLocals);
+
+ }
+
+
+ /**
* Has this context been initialized?
*/
public boolean isInitialized() {
diff --git a/java/org/apache/catalina/loader/LocalStrings.properties b/java/org/apache/catalina/loader/LocalStrings.properties
index 1c4b0403b..be1b06972 100644
--- a/java/org/apache/catalina/loader/LocalStrings.properties
+++ b/java/org/apache/catalina/loader/LocalStrings.properties
@@ -38,8 +38,10 @@ webappClassLoader.clearReferencesResourceBundlesCount=Removed [{0}] ResourceBund
webappClassLoader.clearReferencesResourceBundlesFail=Failed to clear ResourceBundle references for web application [{0}]
webappClassLoader.clearRmiInfo=Failed to find class sun.rmi.transport.Target to clear context class loader for web application [{0}]. This is expected on non-Sun JVMs.
webappClassLoader.clearRmiFail=Failed to clear context class loader referenced from sun.rmi.transport.Target for web application [{0}]
-webappClassLoader.clearThreadLocalDebug=The web application [{0}] created a ThreadLocal with key of type [{1}] (value [{2}]). The ThreadLocal has been correctly set to null and the key will be removed by GC. However, to simplify the process of tracing memory leaks, the key has been forcibly removed.
-webappClassLoader.clearThreadLocal=The web application [{0}] created a ThreadLocal with key of type [{1}] (value [{2}]) and a value of type [{3}] (value [{4}]) but failed to remove it when the web application was stopped. To prevent a memory leak, the ThreadLocal has been forcibly removed.
+webappClassLoader.clearThreadLocalDebug=The web application [{0}] created a ThreadLocal with key of type [{1}] (value [{2}]). The ThreadLocal has been correctly set to null and the key will be removed by GC.
+webappClassLoader.clearThreadLocal=The web application [{0}] created a ThreadLocal with key of type [{1}] (value [{2}]) and a value of type [{3}] (value [{4}]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak.
+webappClassLoader.clearThreadLocalDebugClear=To simplify the process of tracing memory leaks, the key has been forcibly removed.
+webappClassLoader.clearThreadLocalClear=To prevent a memory leak, the ThreadLocal has been forcibly removed.
webappClassLoader.clearThreadLocalFail=Failed to clear ThreadLocal references for web application [{0}]
webappClassLoader.stopThreadFail=Failed to terminate thread named [{0}] for web application [{1}]
webappClassLoader.stopTimerThreadFail=Failed to terminate TimerThread named [{0}] for web application [{1}]
diff --git a/java/org/apache/catalina/loader/WebappClassLoader.java b/java/org/apache/catalina/loader/WebappClassLoader.java
index a7527e117..2aee5422d 100644
--- a/java/org/apache/catalina/loader/WebappClassLoader.java
+++ b/java/org/apache/catalina/loader/WebappClassLoader.java
@@ -446,6 +446,15 @@ public class WebappClassLoader
private boolean clearReferencesStopThreads = false;
/**
+ * Should Tomcat attempt to clear any ThreadLocal objects that are instances
+ * of classes loaded by this class loader. Failure to remove any such
+ * objects will result in a memory leak on web application stop, undeploy or
+ * reload. It is disabled by default since the clearing of the ThreadLocal
+ * objects is not performed in a thread-safe manner.
+ */
+ private boolean clearReferencesThreadLocals = false;
+
+ /**
* Should Tomcat call {@link org.apache.juli.logging.LogFactory#release()}
* when the class loader is stopped? If not specified, the default value
* of true is used. Changing the default setting is likely to
@@ -676,6 +685,27 @@ public class WebappClassLoader
}
+
+
+ /**
+ * Return the clearReferencesThreadLocals flag for this Context.
+ */
+ public boolean getClearReferencesThreadLocals() {
+ return (this.clearReferencesThreadLocals);
+ }
+
+
+ /**
+ * Set the clearReferencesThreadLocals feature for this Context.
+ *
+ * @param clearReferencesThreadLocals The new flag value
+ */
+ public void setClearReferencesThreadLocals(
+ boolean clearReferencesThreadLocals) {
+ this.clearReferencesThreadLocals = clearReferencesThreadLocals;
+ }
+
+
/**
* Set the clearReferencesLogFactoryRelease feature for this Context.
*
@@ -2254,16 +2284,26 @@ public class WebappClassLoader
log.debug(sm.getString(
"webappClassLoader.clearThreadLocalDebug",
args));
+ if (clearReferencesThreadLocals) {
+ log.debug(sm.getString(
+ "webappClassLoader.clearThreadLocalDebugClear"));
+ }
}
} else {
log.error(sm.getString(
"webappClassLoader.clearThreadLocal",
args));
+ if (clearReferencesThreadLocals) {
+ log.info(sm.getString(
+ "webappClassLoader.clearThreadLocalClear"));
+ }
}
- if (key == null) {
- staleEntriesCount++;
- } else {
- mapRemove.invoke(map, key);
+ if (clearReferencesThreadLocals) {
+ if (key == null) {
+ staleEntriesCount++;
+ } else {
+ mapRemove.invoke(map, key);
+ }
}
}
}
diff --git a/java/org/apache/catalina/loader/WebappLoader.java b/java/org/apache/catalina/loader/WebappLoader.java
index f1e248546..bda7a70b9 100644
--- a/java/org/apache/catalina/loader/WebappLoader.java
+++ b/java/org/apache/catalina/loader/WebappLoader.java
@@ -596,6 +596,8 @@ public class WebappLoader extends LifecycleBase
((StandardContext) container).getClearReferencesStatic());
classLoader.setClearReferencesStopThreads(
((StandardContext) container).getClearReferencesStopThreads());
+ classLoader.setClearReferencesThreadLocals(
+ ((StandardContext) container).getClearReferencesThreadLocals());
}
for (int i = 0; i < repositories.length; i++) {
diff --git a/webapps/docs/config/context.xml b/webapps/docs/config/context.xml
index ca7f227ee..679d253d6 100644
--- a/webapps/docs/config/context.xml
+++ b/webapps/docs/config/context.xml
@@ -380,6 +380,15 @@
default value of false will be used.
If true, Tomcat attempts to clear any ThreadLocal
+ objects that are instances of classes loaded by this class loader.
+ Failure to remove any such objects will result in a memory leak on web
+ application stop, undeploy or reload. If not specified, the default
+ value of false will be used since the clearing of the
+ ThreadLocal objects is not performed in a thread-safe manner.
Whether the context should process TLDs on startup. The default is true. The false setting is intended for special cases -- 2.11.0