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!
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With