Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, 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!

like image 428
Igor Avatar asked Jun 16 '11 08:06

Igor


1 Answers

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>
like image 129
Igor Avatar answered Oct 13 '22 00:10

Igor