You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Marcel Stör <ma...@frightanic.com> on 2010/09/23 21:54:17 UTC

ThreadLocal from MDC not cleared

Tomcat >=6.0.24 comes with a pre-configured "memory leak detection" listener that cleans up when an application is shutdown.

It particularly complains about ThreadLocal in log4j's MDC (many such log statements):

A web application created a ThreadLocal with key of type [org.apache.log4j.helpers.ThreadLocalMap] (value [org.apache.log4j.helpers.ThreadLocalMap@5212bc]) and a value of type [java.util.Hashtable] (value [{}]) but failed to remove it when the web application was stopped. To prevent a memory leak, the ThreadLocal has been forcibly removed.

One can see that the Hashtable in the ThreadLocal is empty but that doesn't seem to be enough as the ThreadLocal itself is not empty....

I'm using the MDC in a ServletContextFilter like so:

try {
  if (request instanceof HttpServletRequest) {
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    Principal principal = httpServletRequest.getUserPrincipal();
    if (principal != null) {
      MDC.put(MDC_PRINCIPAL_KEY, principal.getName());
    }
  }
  chain.doFilter(request, response);
} finally {
  MDC.remove(MDC_PRINCIPAL_KEY);
}


Do I have a chance to properly clear the ThreadLocal variable?

Regards,
Marcel

-- 
Marcel Stör, http://www.frightanic.com
Couchsurfing: http://www.couchsurfing.com/people/marcelstoer
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org