logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ralph Goers <ralph.go...@dslextreme.com>
Subject Re: [Bug 50486] Memoryleak - org.apache.log4j.helpers.ThreadLocalMap
Date Wed, 30 May 2012 05:59:47 GMT
As I see it, the problem is that at least some of the users seem to be expecting Log4j to magically
know when to call clear. It can't. The application has to do it.

All the MDC is is a ThreadLocal where each Thread has its own Map associated with the ThreadLocal.
You can get and put all you want. In a webapp, of course, if you add stuff to the MDC at the
beginning of a request then you need to remove it all at the end of the request.  The normal
pattern is

MDC.put(key, value);
try {
   call servlet
} finally {
  MDC.remove(key);
}

The problem with this is that it still leaves an empty Map in the ThreadLocal. So when remove()
removes the last element from the Map then the Map is removed from the ThreadLocal.  

FWIW, Logback suffered a similar issue in http://jira.qos.ch/browse/LBCLASSIC-183. 

Ralph


On May 29, 2012, at 10:31 PM, Christian Grobmeier wrote:

> Bringing this to the mailinglist.
> 
> Ralph, I understand what you explained below, but I am lost on how to
> fix it. Do you have any more suggestions?
> I must actually admit that I am not familiar with MDC at all, probably
> that's my problem with it.
> 
> On Wed, May 30, 2012 at 7:26 AM,  <bugzilla@apache.org> wrote:
>> https://issues.apache.org/bugzilla/show_bug.cgi?id=50486
>> 
>> --- Comment #37 from Ralph Goers <Ralph.Goers@dslextreme.com> ---
>> Yes, that is what clear is for.
>> 
>> I don't see how Hierarchy.shutdown() can call MDC.clear(). Well, it could but
>> it would only clear the thread the shutdown is performed on which isn't likely
>> to help much.
>> 
>> Item 2 is likely to cause many problems.  MDC.clear only clears the current
>> thread's ThreadLocal. Reinitializing the MDC will cause problems for all the
>> other threads.
>> 
>> --
>> You are receiving this mail because:
>> You are the assignee for the bug.
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
>> For additional commands, e-mail: log4j-dev-help@logging.apache.org
>> 
> 
> 
> 
> -- 
> http://www.grobmeier.de
> https://www.timeandbill.de
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-dev-help@logging.apache.org
> 


Mime
View raw message