I found a solution for memory leak.
You must run the ServletContextListener , as shown below:
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, register the listener in web.xml:
<listener> <listener-class>org.example.MyServletContextListener</listener-class> </listener>
source share