<< Adding a custom test phase to Grails 2.3 | Home

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 {
    ...

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        ...
        createContext(...)
        ...
    }

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

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




Send a TrackBack