tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r909134 - in /tomcat/trunk/java/org/apache/catalina: core/StandardHost.java core/mbeans-descriptors.xml loader/WebappClassLoader.java
Date Thu, 11 Feb 2010 20:32:10 GMT
Author: markt
Date: Thu Feb 11 20:32:09 2010
New Revision: 909134

URL: http://svn.apache.org/viewvc?rev=909134&view=rev
Log:
Add rudimentary detection for PermGen memory leaks on web application reload. Only available
via JMX for now. Next step is to add it to the manager app.

Modified:
    tomcat/trunk/java/org/apache/catalina/core/StandardHost.java
    tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml
    tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java

Modified: tomcat/trunk/java/org/apache/catalina/core/StandardHost.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardHost.java?rev=909134&r1=909133&r2=909134&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/StandardHost.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/StandardHost.java Thu Feb 11 20:32:09 2010
@@ -19,6 +19,11 @@
 package org.apache.catalina.core;
 
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.WeakHashMap;
+
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
 
@@ -27,6 +32,7 @@
 import org.apache.catalina.Host;
 import org.apache.catalina.LifecycleException;
 import org.apache.catalina.Valve;
+import org.apache.catalina.loader.WebappClassLoader;
 import org.apache.catalina.startup.HostConfig;
 import org.apache.catalina.valves.ValveBase;
 import org.apache.tomcat.util.modeler.Registry;
@@ -162,6 +168,14 @@
      */
      private boolean createDirs = true;
 
+     
+     /**
+      * Track the class loaders for the child web applications so memory leaks
+      * can be detected.
+      */
+     private Map<ClassLoader, String> childClassLoaders =
+         new WeakHashMap<ClassLoader, String>();
+
     // ------------------------------------------------------------- Properties
 
 
@@ -565,11 +579,42 @@
             throw new IllegalArgumentException
                 (sm.getString("standardHost.notContext"));
         super.addChild(child);
-
+        
+        // Record a reference to the context's class loader to aid memory leak
+        // detection
+        if (child.getLoader() != null) {
+            childClassLoaders.put(child.getLoader().getClassLoader(),
+                    child.getName());
+        }
     }
 
 
     /**
+     * Attempt to identify the contexts that have a class loader memory leak.
+     * This is usually triggered on context reload. Note: This method attempts
+     * to force a full garbage collection. This should be used with extreme
+     * caution on a production system.
+     */
+    public String[] findReloadedContextMemoryLeaks() {
+        
+        System.gc();
+        
+        List<String> result = new ArrayList<String>();
+        
+        for (Map.Entry<ClassLoader, String> entry :
+                childClassLoaders.entrySet()) {
+            ClassLoader cl = entry.getKey();
+            if (cl instanceof WebappClassLoader) {
+                if (!((WebappClassLoader) cl).isStarted()) {
+                    result.add(entry.getValue());
+                }
+            }
+        }
+        
+        return result.toArray(new String[result.size()]);
+    }
+
+    /**
      * Return the set of alias names for this Host.  If none are defined,
      * a zero length array is returned.
      */

Modified: tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml?rev=909134&r1=909133&r2=909134&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml (original)
+++ tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml Thu Feb 11 20:32:09
2010
@@ -541,6 +541,12 @@
     <operation name="stop" description="Stop" impact="ACTION" returnType="void" />
     <operation name="init" description="Init" impact="ACTION" returnType="void" />
     <operation name="destroy" description="Destroy" impact="ACTION" returnType="void"
/>
+    
+    <operation name="findReloadedContextMemoryLeaks"
+               description="Provide a list of contexts that have leaked memory on reload.
This will attempt to force a full garbage collection. Use with extreme caution on prouction
systems."
+               impact="ACTION"
+               returnType="[Ljava.lang.String;" />
+
   </mbean>
   
   <mbean name="StandardHostValve"

Modified: tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java?rev=909134&r1=909133&r2=909134&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java (original)
+++ tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java Thu Feb 11 20:32:09
2010
@@ -1661,6 +1661,10 @@
     }
 
 
+    public boolean isStarted() {
+        return started;
+    }
+
     /**
      * Stop the class loader.
      *



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message