Uploaded image for project: 'SLF4J'
  1. SLF4J
  2. SLF4J-257

Memory Leak: Clearing MDC context with Log4J


    • Type: Bug
    • Status: Open
    • Resolution: Unresolved
    • Affects Version/s: 1.6.x
    • Fix Version/s: None
    • Component/s: Implementations
    • Labels:
    • Environment:

      Operating System: All
      Platform: All


      Recently after a long time log4j got it's problem with clearing the MDC context and the underlying ThreadLocal Hashmap fixed. S. https://issues.apache.org/bugzilla/show_bug.cgi?id=50486

      This fixed a long running Problem with Tomcat freeing memory on reload.

      In order to do so, log4j's MDC got the clear method with 1.2.16 and with 1.2.17 fixed it so that it works.

      Log4j has an MDC adaptor for Log4J and it's own clear method. But instead of calling Log4J's clear it implements essentially (MDC.getContext()).clear() which only clears the HashMap but doesn't remove it from the ThreadLocal context resulting in a MemroryLeak.

      Short workaround: Instead of calling slf4j's MDC.clear() call log4j's MDC.clear() (but that is the exact opositit of what slf4j is made for)

      Simple Fix: change log4j's mdc adaptor to call MDC.clear() instead of MDC.getContext().clear().

      More complex fix: If backward compatibility is an issue (like it was für log4j) than log4js solution can be applied here: Instead of calling MDC.clear() directly call it via reflexion if it exists and otherwise keep calling MDC.getContext().clear().




            • Assignee:
              slf4j-dev SLF4J developers list
              f.bantner@axon-e.de f.bantner
            • Votes:
              2 Vote for this issue
              4 Start watching this issue


              • Created: