<< January 2014 | Home | March 2014 >>

Fixing EhCache / Tomcat memory leak warnings.

We were receiving messages such as this:

The web application [/services] appears to have started a thread named [net.sf.ehcache.CacheManager@37ce6a10] but has failed to stop it. This is very likely to create a memory leak.

The solution, in our groovy based spring web app that uses annotation based configuration, was to register the net.sf.ehcache.constructs.web.ShutdownListener class with the context in our class that extends WebApplicationInitializer via an overridden onStartup() method, something like this:

import net.sf.ehcache.constructs.web.ShutdownListener
import org.springframework.web.WebApplicationInitializer

public class CustomWebApplicationInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {

    private AnnotationConfigWebApplicationContext createContext(final Class... annotatedClasses) {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext()
        return context

More information can be found here: http://ehcache.org/documentation/operations/shutdown