More memory leak protection. This one is particularly nasty as profilers don't appear...
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 25 Nov 2009 23:56:30 +0000 (23:56 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 25 Nov 2009 23:56:30 +0000 (23:56 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@884341 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java
java/org/apache/catalina/core/LocalStrings.properties
webapps/docs/config/listeners.xml

index 4554f8f..13eac01 100644 (file)
@@ -23,6 +23,8 @@ import java.net.URL;
 import java.net.URLConnection;
 
 import javax.imageio.ImageIO;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
 
 import org.apache.catalina.Lifecycle;
 import org.apache.catalina.LifecycleEvent;
@@ -75,6 +77,17 @@ public class JreMemoryLeakPreventionListener implements LifecycleListener {
         this.urlCacheProtection = urlCacheProtection;
     }
 
+    /**
+     * 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. 
+     */
+    protected boolean xmlParsingProtection = true;
+    public boolean isXmlParsingProtection() { return xmlParsingProtection; }
+    public void setXmlParsingProtection(boolean xmlParsingProtection) {
+        this.xmlParsingProtection = xmlParsingProtection;
+    }
+    
     @Override
     public void lifecycleEvent(LifecycleEvent event) {
         // Initialise these classes when Tomcat starts
@@ -122,7 +135,23 @@ public class JreMemoryLeakPreventionListener implements LifecycleListener {
                             "jreLeakListener.jarUrlConnCacheFail"), e);
                 } catch (IOException e) {
                     log.error(sm.getString(
-                    "jreLeakListener.jarUrlConnCacheFail"), e);
+                            "jreLeakListener.jarUrlConnCacheFail"), e);
+                }
+            }
+            
+            /*
+             * Haven't got to the root of what is going on with this leak but if
+             * a web app is the first to make the calls below the web
+             * application class loader will be pinned in memory.
+             */
+            if (xmlParsingProtection) {
+                DocumentBuilderFactory factory =
+                    DocumentBuilderFactory.newInstance();
+                try {
+                    factory.newDocumentBuilder();
+                } catch (ParserConfigurationException e) {
+                    log.error(sm.getString(
+                            "jreLeakListener.xmlParseFail"), e);
                 }
             }
         }
index 5fbad19..f673603 100644 (file)
@@ -66,6 +66,7 @@ httpHostMapper.container=This container is not a StandardHost
 interceptorValve.alreadyStarted=InterceptorValve has already been started
 interceptorValve.notStarted=InterceptorValve has not yet been started
 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}
 naming.bindFailed=Failed to bind object: {0}
 naming.jmxRegistrationFailed=Failed to register in JMX: {0}
index 9113892..f882a9d 100644 (file)
@@ -256,6 +256,13 @@ service:jmx:rmi://&lt;hostname&gt;:10002/jndi/rmi://&lt;hostname&gt;:10001/jmxrm
         case by case basis as required. Defaults to <code>true</code>.</p>
       </attribute>
 
+      <attribute name="xmlParsingProtection" required="false">
+        <p>Enables protection so that parsing XML files within a web application
+        does not result in a memopry leak. Note that memory profilers may not
+        display the GC root associated with this leak making it particularly
+        hard to diagnose. Defaults to <code>true</code>.</p>
+      </attribute>
+
     </attributes>
 
   </subsection>