Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-2366

LoggerContext leak using Log4jLoggerFactory



    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.11.0
    • 2.12.1
    • SLF4J Bridge
    • None


      Hi all!

      We are using log4j2 as the logging framework for our applications.
      This is done by using the log4j implementation provided for slf4j.

      The problem we are seeing is that after many application redeploys, many LoggerContexts are still referenced, even after stopping them.

      I've taken many heap dumps of our standalone server and found that many of these references are kept by a WeakHashMap<> instantiated in the Log4jLoggerFactory. ('registry' attribute of org.apache.logging.log4j.spi.AbstractLoggerAdapter)
      What happens is that, even when the Map is a WeakHashMap (Where the keys should be weakly referenced), it will never be cleared because it's values keep references to those same keys.

      If you check the heap dumps that i've provided, you can see that most of the LoggerContexts (org.mule.runtime.module.launcher.log4j2.MuleLoggerContext) states are: STOPPED and even it's loggers configurations are updated to NullConfiguration, but the references are kept.

      On the other hand, i could remove the references to their contexts for the loggers ('context' attribute of org.apache.logging.log4j.core.Logger) when those contexts are stopped but the field is final and private, which makes it impossible to change for an child class.

      For the heapdumps, i took them as follows:

      • deployed application
      • dumped patched_before.hprof
      • redeployed app 10 times
      • dumped patched_after.hprof
      • waited an hour
      • dumped patched_after_waited.hprof

      You can see that in patched_before.hprof there are only 4 LoggerContext instances while in patched_after.hprof and patched_after_waited.hprof  there are around 16, most of them referenced by a logger that is linked to a value in the WeakHashMap mentioned above.

      I think that you should, either provide a way to remove entries from that 'registry', or check for circular dependencies somehow so that they are removed by the GC.



        1. minimal_example.7z
          5.02 MB
          Luciano Raineri Marchina
        2. patched_after.hprof.7z
          28.41 MB
          Luciano Raineri Marchina
        3. patched_after_waited.hprof.7z
          29.30 MB
          Luciano Raineri Marchina
        4. patched_before.hprof.7z
          25.42 MB
          Luciano Raineri Marchina



            Unassigned Unassigned
            lucianorm Luciano Raineri Marchina
            1 Vote for this issue
            4 Start watching this issue