开发者

sun.net.www.http.HttpClient memory leak in Tomcat 6

I'm using Tomcat 6.0.18. After undeployemnt of my app, HttpClient appears to be holding a reference to WebappClassLoader, hence causing the memory leak.

After some investigation开发者_Go百科, I've found solution in Tomcat 7.0.6, JreMemoryLeakPreventionListener with keepAliveProtection attribute. But this approach is not working with Tomcats 6 (I've customized JreMemoryLeakPreventionListener, in order to add support for this attribute).

Does anybody have a solution how to fix this leak in Tomcat 6? Thanx!


I've found the solution for memory leak.

One must make implementation of ServletContextListener, as following:

package org.example;

public class MyServletContextListener implements ServletContextListener {

    public void contextDestroyed(ServletContextEvent sce) {
         tomcatLeakPreventionForHttpClient();
    }

    private void tomcatLeakPreventionForHttpClient() {
        try {
            final Field kac = HttpClient.class.getDeclaredField("kac");
            kac.setAccessible(true);
            final Field keepAliveTimer = KeepAliveCache.class.getDeclaredField("keepAliveTimer");
            keepAliveTimer.setAccessible(true);

            final Thread t = (Thread) keepAliveTimer.get(kac.get(null));
            if(t.getContextClassLoader() == Thread.currentThread().getContextClassLoader()) {
                t.setContextClassLoader(ClassLoader.getSystemClassLoader());
            }
        } catch(final Exception e) {
        }
    }

    public void contextInitialized(ServletContextEvent event) {
    }

}

and, of course, to register listener in web.xml:

  <listener>
    <listener-class>org.example.MyServletContextListener</listener-class>
  </listener>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜