tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r828196 - in /tomcat/trunk: conf/server.xml java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java
Date Wed, 21 Oct 2009 20:36:23 GMT
Author: markt
Date: Wed Oct 21 20:36:23 2009
New Revision: 828196

URL: http://svn.apache.org/viewvc?rev=828196&view=rev
Log:
Somewhat ironically, the call to java.beans.Introspector.flushCaches() that is meant to prevent
memory leaks now causes a leak on reload due to a change in 1.6.0_15 onwards.
Add a listener that prevents the leak.

Added:
    tomcat/trunk/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java
Modified:
    tomcat/trunk/conf/server.xml

Modified: tomcat/trunk/conf/server.xml
URL: http://svn.apache.org/viewvc/tomcat/trunk/conf/server.xml?rev=828196&r1=828195&r2=828196&view=diff
==============================================================================
--- tomcat/trunk/conf/server.xml (original)
+++ tomcat/trunk/conf/server.xml Wed Oct 21 20:36:23 2009
@@ -25,6 +25,8 @@
   <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
   <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html
-->
   <Listener className="org.apache.catalina.core.JasperListener" />
+  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
+  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
   <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
   <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
   <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

Added: tomcat/trunk/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java?rev=828196&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java (added)
+++ tomcat/trunk/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java Wed Oct
21 20:36:23 2009
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.catalina.core;
+
+import javax.imageio.ImageIO;
+
+import org.apache.catalina.Lifecycle;
+import org.apache.catalina.LifecycleEvent;
+import org.apache.catalina.LifecycleListener;
+
+/**
+ * Provide a workaround for known places where the Java Runtime environment uses
+ * the context class loader to load a singleton as this will cause a memory leak
+ * if a web application class loader happens to be the context class loader at
+ * the time. The work-around is to initialise these singletons when Tomcat's
+ * common class loader is the context class loader.
+ */
+public class JreMemoryLeakPreventionListener implements LifecycleListener {
+
+    @Override
+    public void lifecycleEvent(LifecycleEvent event) {
+        // Initialise these classes when Tomcat starts
+        if (Lifecycle.INIT_EVENT.equals(event.getType())) {
+            /*
+             * Several components end up calling:
+             * sun.awt.AppContext.getAppContext()
+             * 
+             * Those libraries / components known to trigger memory leaks due to
+             * eventual calls to getAppContext() are:
+             * 
+             * - Google Web Toolkit via its use of javax.imageio
+             * - Tomcat via its use of java.beans.Introspector.flushCaches() in
+             *   1.6.0_15 onwards
+             * - others TBD
+             */
+            
+            // Trigger a call to sun.awt.AppContext.getAppContext(). This will
+            // pin the common class loader in memory but that shouldn't be an
+            // issue.
+            ImageIO.getCacheDirectory();
+            
+        }
+    }
+
+}



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


Mime
View raw message