tomcat memory leak

 

tomcat memory leak

 

Mar 18, 2010 11:13:07 PM org.apache.catalina.core.ApplicationContext log
INFO: HTMLManager: stop: Stopping web application at '/testWeb'
Mar 18, 2010 11:13:07 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: A web application appears to have started a thread named [leakingThread] but has failed to stop it. This is very likely to create a memory leak.
public class LeakingServlet extends HttpServlet {
        private Thread leakingThread;

        protected void doGet(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException {

                if (leakingThread == null) {
                        synchronized (this) {
                                if (leakingThread == null) {
                                        leakingThread = new Thread("leakingThread") {

                                                @Override
                                                public void run() {
                                                        synchronized (this) {
                                                                try {
                                                                        this.wait();
                                                                } catch (InterruptedException e) {
                                                                        e.printStackTrace();
                                                                }
                                                        }
                                                }
                                        };
                                        leakingThread.setDaemon(true);
                                        leakingThread.setContextClassLoader(null);
                                        leakingThread.start();
                                }
                        }
                }
                response.getWriter().println("Hello world!");
        }
}
 需要把子线程的classloader设置为空(eakingThread.setContextClassLoader(null);),
不受主线程的管理,当tomcatstop的时候就不会检测
内存泄露了。
http://wiki.apache.org/tomcat/MemoryLeakProtection#cclThreadSpawnedByWebApp

 

你可能感兴趣的:(memory leak)