From: markt Date: Mon, 8 Feb 2010 13:29:13 +0000 (+0000) Subject: More memory leak protection on reload. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=963fdc3a5d553326c7327d145f7f37dbe64d1fb4;p=tomcat7.0 More memory leak protection on reload. Use of java.util.Timer git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@907652 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/java/org/apache/catalina/loader/LocalStrings.properties b/java/org/apache/catalina/loader/LocalStrings.properties index 4d326372f..0919bbbc4 100644 --- a/java/org/apache/catalina/loader/LocalStrings.properties +++ b/java/org/apache/catalina/loader/LocalStrings.properties @@ -40,8 +40,10 @@ webappClassLoader.clearRmiFail=Failed to clear context class loader referenced f 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} diff --git a/java/org/apache/catalina/loader/WebappClassLoader.java b/java/org/apache/catalina/loader/WebappClassLoader.java index 625b2e8aa..a2bcb0ce1 100644 --- a/java/org/apache/catalina/loader/WebappClassLoader.java +++ b/java/org/apache/catalina/loader/WebappClassLoader.java @@ -47,6 +47,7 @@ import java.util.Iterator; 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 @@ public class WebappClassLoader * 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 - * false will be used. + * false 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 @@ public class WebappClassLoader 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 @@ public class WebappClassLoader } + 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();