vy opened a new issue, #3430:
URL: https://github.com/apache/logging-log4j2/issues/3430

   `InternalLoggerRegistry` is essentially a `Map<MessageFactory, Map<String, 
WeakReference<Logger>>>`. [As 
reported](/apache/logging-log4j2/issues/3399#issuecomment-2626946587) by 
@tristantarrant, stale entries are not expunged in its current form. That is, 
the inner map does not get compacted for entries whose associated 
`WeakReference`s are reclaimed by the garbage collector. This might cause high 
memory usage if an excessive amount of ephemeral loggers get registered.
   
   Reclaimed references can be tracked by a `ReferenceQueue` passed to the 
`WeakReference::new` and processed as follows:
   
   ```java
   private final ReferenceQueue<Logger> staleLoggerRefs = new 
ReferenceQueue<>();
   
   private void expungeStaleEntries() {
       Reference<? extends Logger> loggerRef;
       while ((loggerRef = staleLoggerRefs.poll()) != null) {
           Logger logger = loggerRef.get();
           if (logger != null) {
               removeLogger(logger);
           }
       }
   }
   ```
   
   When to run `expungeStaleEntries()` should be decided with care, since 
`removeLogger()` mutates the registry map.
   
   ### On method invocation
   
   All `ILR` methods (`getLogger()`, `computeIfAbsent()`, etc.) can first try 
to expunge stale entries.
   This approach is exercised by `WeakHashMap` itself and in #3427.
   
   **Pros:**
   
   - Implies no changes besides `ILR`
   
   **Cons:**
   
   - Read-only operations, which are accessed most, might try to acquire the 
write-lock.
   - If `ILR` is not accessed, stale entries will still be kept. Consider an 
application where all `Logger`s are assigned, and a temporary operation creates 
excessive amount of loggers and completes. All created temporary `Logger` 
instances will be reclaimed by GC, yet will continue taking space for the 
associated `Map.Entry<String, WeakReference<Logger>>`.
   
   ### By a janitor thread
   
   `ILR` can start a janitor thread listening on the `ReferenceQueue<Logger>`.
   
   **Cons:**
   
   - `ILR` must have a life cycle (to start/stop the janitor thread) managed by 
`LoggerContext`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@logging.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to