From 6972b3996cb1df648d4a874d071f0531a856ef45 Mon Sep 17 00:00:00 2001
From: markt sun.misc.GC.requestLatency(long) is triggered by a web
+ * application. This first call will start a GC Daemon thread with the
+ * thread's context class loader configured to be the web application class
+ * loader. Defaults to true.
+ */
+ private boolean gcDaemonProtection = true;
+ public boolean isGcDaemonProtection() { return gcDaemonProtection; }
+ public void setGcDaemonProtection(boolean gcDaemonProtection) {
+ this.gcDaemonProtection = gcDaemonProtection;
+ }
+
@Override
public void lifecycleEvent(LifecycleEvent event) {
// Initialise these classes when Tomcat starts
@@ -150,8 +165,36 @@ public class JreMemoryLeakPreventionListener implements LifecycleListener {
try {
factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
- log.error(sm.getString(
- "jreLeakListener.xmlParseFail"), e);
+ log.error(sm.getString("jreLeakListener.xmlParseFail"), e);
+ }
+ }
+
+ /*
+ * Several components end up calling:
+ * sun.misc.GC.requestLatency(long)
+ *
+ * Those libraries / components known to trigger memory leaks due to
+ * eventual calls to requestLatency(long) are:
+ * - javax.management.remote.rmi.RMIConnectorServer.start()
+ */
+ if (gcDaemonProtection) {
+ try {
+ Class> clazz = Class.forName("sun.misc.GC");
+ Method method = clazz.getDeclaredMethod("requestLatency",
+ new Class[] {long.class});
+ method.invoke(null, Long.valueOf(3600000));
+ } catch (ClassNotFoundException e) {
+ log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
+ } catch (SecurityException e) {
+ log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
+ } catch (NoSuchMethodException e) {
+ log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
+ } catch (IllegalArgumentException e) {
+ log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
+ } catch (IllegalAccessException e) {
+ log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
+ } catch (InvocationTargetException e) {
+ log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
}
}
}
diff --git a/java/org/apache/catalina/core/LocalStrings.properties b/java/org/apache/catalina/core/LocalStrings.properties
index 993babdf7..bf961a548 100644
--- a/java/org/apache/catalina/core/LocalStrings.properties
+++ b/java/org/apache/catalina/core/LocalStrings.properties
@@ -65,6 +65,7 @@ httpEngineMapper.container=This container is not a StandardEngine
httpHostMapper.container=This container is not a StandardHost
interceptorValve.alreadyStarted=InterceptorValve has already been started
interceptorValve.notStarted=InterceptorValve has not yet been started
+jreLeakListener.gcDaemonFail=Failed to trigger creation of the GC Daemon thread during Tomcat start to prevent possible memory leaks. This is expected on non-Sun JVMs.
jreLeakListener.jarUrlConnCacheFail=Failed to disable Jar URL connection caching by default
jreLeakListener.xmlParseFail=Error whilst attempting to prevent memory leaks during XML parsing
naming.wsdlFailed=Failed to find wsdl file: {0}
diff --git a/webapps/docs/config/listeners.xml b/webapps/docs/config/listeners.xml
index f882a9d18..521718a50 100644
--- a/webapps/docs/config/listeners.xml
+++ b/webapps/docs/config/listeners.xml
@@ -244,7 +244,17 @@ service:jmx:rmi://<hostname>:10002/jndi/rmi://<hostname>:10001/jmxrm
application do not result in a memory leak. Note that a call to this
method will be triggered as part of the web application stop process so
it is strongly recommended that this protection is enabled. The default
- is true
true.
+
+
+ Enables protection so that calls to
+ sun.misc.GC.requestLatency(long) triggered by a web
+ application do not result in a memory leak. Use of RMI is likely to
+ trigger a call to this method. A side effect of enabling this protection
+ is the creation of a thread named "GC Daemon". The protection is uses
+ reflection to access internal Sun classes and may generate errors on
+ startup on non-Sun JVMs. The default is true.