Author: markt Date: Mon Feb 8 13:29:13 2010 New Revision: 907652 URL: http://svn.apache.org/viewvc?rev=907652&view=rev Log: More memory leak protection on reload. Use of java.util.Timer
Modified: tomcat/trunk/java/org/apache/catalina/loader/LocalStrings.properties tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java Modified: tomcat/trunk/java/org/apache/catalina/loader/LocalStrings.properties URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/loader/LocalStrings.properties?rev=907652&r1=907651&r2=907652&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/loader/LocalStrings.properties (original) +++ tomcat/trunk/java/org/apache/catalina/loader/LocalStrings.properties Mon Feb 8 13:29:13 2010 @@ -40,8 +40,10 @@ webappClassLoader.clearThreadLocal=A web application created a ThreadLocal with key of type [{0}] (value [{1}]) and a value of type [{2}] (value [{3}]) but failed to remove it when the web application was stopped. To prevent a memory leak, the ThreadLocal has been forcibly removed. webappClassLoader.clearThreadLocalFail=Failed to clear ThreadLocal references webappClassLoader.stopThreadFail=Failed to terminate thread named [{0}] +webappClassLoader.stopTimerThreadFail=Failed to terminate TimerThread named [{0}] webappClassLoader.validationErrorJarPath=Unable to validate JAR entry with name {0} webappClassLoader.warnThread=A web application appears to have started a thread named [{0}] but has failed to stop it. This is very likely to create a memory leak. +webappClassLoader.warnTimerThread=A web application appears to have started a TimerThread named [{0}] via the java.util.Timer API but has failed to stop it. To prevent a memory leak, the timer (and hence the associated thread) has been forcibly cancelled. webappClassLoader.wrongVersion=(unable to load class {0}) webappLoader.addRepository=Adding repository {0} webappLoader.deploy=Deploying class repositories to work directory {0} 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=907652&r1=907651&r2=907652&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java (original) +++ tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java Mon Feb 8 13:29:13 2010 @@ -47,6 +47,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.TimerTask; import java.util.Vector; import java.util.concurrent.ThreadPoolExecutor; import java.util.jar.Attributes; @@ -430,7 +431,9 @@ * instability. As such, enabling this should be viewed as an option of last * resort in a development environment and is not recommended in a * production environment. If not specified, the default value of - * <code>false</code> will be used. + * <code>false</code> will be used. Note that instances of + * java.util.TimerThread will always be terminate since a safe method exists + * to do so. */ private boolean clearReferencesStopThreads = false; @@ -1960,6 +1963,13 @@ continue; } + // TimerThread is not normally visible + if (thread.getClass().getName().equals( + "java.util.TimerThread")) { + clearReferencesStopTimerThread(thread); + continue; + } + log.error(sm.getString("webappClassLoader.warnThread", thread.getName())); @@ -2017,6 +2027,53 @@ } + private void clearReferencesStopTimerThread(Thread thread) { + + // Need to get references to: + // - newTasksMayBeScheduled field + // - queue field + // - queue.clear() + + try { + Field newTasksMayBeScheduledField = + thread.getClass().getDeclaredField("newTasksMayBeScheduled"); + newTasksMayBeScheduledField.setAccessible(true); + Field queueField = thread.getClass().getDeclaredField("queue"); + queueField.setAccessible(true); + + Object queue = queueField.get(thread); + + Method clearMethod = queue.getClass().getDeclaredMethod("clear"); + clearMethod.setAccessible(true); + + synchronized(queue) { + newTasksMayBeScheduledField.setBoolean(thread, false); + clearMethod.invoke(queue); + queue.notify(); // In case queue was already empty. + } + + log.error(sm.getString("webappClassLoader.warnTimerThread", + thread.getName())); + + } catch (NoSuchFieldException e) { + log.warn(sm.getString( + "webappClassLoader.stopTimerThreadFail", + thread.getName()), e); + } catch (IllegalAccessException e) { + log.warn(sm.getString( + "webappClassLoader.stopTimerThreadFail", + thread.getName()), e); + } catch (NoSuchMethodException e) { + log.warn(sm.getString( + "webappClassLoader.stopTimerThreadFail", + thread.getName()), e); + } catch (InvocationTargetException e) { + log.warn(sm.getString( + "webappClassLoader.stopTimerThreadFail", + thread.getName()), e); + } + } + private void clearReferencesThreadLocals() { Thread[] threads = getThreads(); --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org