sun. net. www.http. Httpclient has a memory leak in Tomcat 6

I'm using Tomcat 6.0 18. After Undeploying my application, httpclient seems to hold a reference to webappclassloader, resulting in memory leakage

After some investigation, I was in Tomcat 7.0 6. The solution of keepaliveprotection attribute is found in jrememoryleakpreventionlistener However, this method is not applicable to Tomcats 6 (I have customized jrememoryleakpreventionlistener to add support for this attribute)

Does anyone have a solution to fix this vulnerability in Tomcat 6? Thank you list!

Solution

I found a solution to the memory leak

Servletcontextlistener must be executed as follows:

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) {
    }

}

Of course, on the web Register listener in XML:

<listener>
    <listener-class>org.example.Myservletcontextlistener</listener-class>
  </listener>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>