From 6972b3996cb1df648d4a874d071f0531a856ef45 Mon Sep 17 00:00:00 2001 From: markt Date: Tue, 1 Dec 2009 18:44:32 +0000 Subject: [PATCH] More memory leak protection - this time for the GC Daemon thread. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@885860 13f79535-47bb-0310-9956-ffa450edef68 --- .../core/JreMemoryLeakPreventionListener.java | 49 ++++++++++++++++++++-- .../apache/catalina/core/LocalStrings.properties | 1 + webapps/docs/config/listeners.xml | 12 +++++- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java b/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java index f72c64a19..5cb7ca85b 100644 --- a/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java +++ b/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java @@ -18,6 +18,8 @@ package org.apache.catalina.core; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; @@ -80,7 +82,7 @@ public class JreMemoryLeakPreventionListener implements LifecycleListener { /** * XML parsing can pin a web application class loader in memory. This is * particularly nasty as profilers (at least YourKit and Eclispe MAT) don't - * idenitfy any GC roots related to this. + * identify any GC roots related to this. */ private boolean xmlParsingProtection = true; public boolean isXmlParsingProtection() { return xmlParsingProtection; } @@ -88,6 +90,19 @@ public class JreMemoryLeakPreventionListener implements LifecycleListener { this.xmlParsingProtection = xmlParsingProtection; } + /** + * Protect against the memory leak caused when the first call to + * 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

+ is 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.

-- 2.11.0