-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Removes weak references from LoggerRepository
#3199
Conversation
Removes weak references to `Logger`s in `LoggerRepository`. The usage of weak references in `LoggerRepository` might cause `null` to be returned by `LogManager.getLogger()` of all Log4j Core versions up to `2.24.1`. Versions of Log4j API up to `2.24.0` did hold **hard** references to all the registered loggers, so the change will not alter the previous behavior. This PR also inverts the order of the `String` and `MessageFactory` keys to the `LoggerRepository` multi-map to limit the number of internal maps. The external map is a `WeakHashMap` to allow logger-specific message factories to be GC-ed. Closes #3143.
The weak reference to logging-log4j2/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java Lines 526 to 536 in 8ee9387
|
log4j-api/src/main/java/org/apache/logging/log4j/spi/LoggerRegistry.java
Outdated
Show resolved
Hide resolved
In order to preserve backward compatibility and allow
|
How will you implement this while preserving the backward compatibility? |
|
|
A complex PR without tests? Isn't it possible to test this issue to avoid regressions in the future? |
Would it be possible to optionally support not having weak references? I understand why weak references are useful for edge cases like users running with dangerously low heap for their applications. There are also users who have applications that don't need weak references here and would appreciate not risking having NPEs. |
@pjfanning, making loggers garbage-collectible is not only a concern for environments with constrained memory resources. This will also greatly benefit users creating loggers programmatically. |
Fixed in 041e5eb |
😊 ty! |
There should be no such problem. The only place were weak references are used in this PR is the Loggers could become unreachable if the message factory becomes unreachable. However the message factory never ceases to be reachable. In a typical logging-log4j2/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java Lines 533 to 535 in 8ee9387
logging-log4j2/log4j-api/src/main/java/org/apache/logging/log4j/spi/LoggerRegistry.java Lines 159 to 170 in bf79552
According to the blog post shared by Volkan this means that the message factory will be reachable up to line 166. However, that line returns a |
@vy, Do you have any comments regarding
|
Removes weak references to `Logger`s in `LoggerRepository`. The usage of weak references in `LoggerRepository` might cause `null` to be returned by `LogManager.getLogger()` of all Log4j Core versions up to `2.24.1`. Versions of Log4j API up to `2.24.0` did hold **hard** references to all the registered loggers, so the change will not alter the previous behavior. This PR also inverts the order of the `String` and `MessageFactory` keys to the `LoggerRepository` multi-map to limit the number of internal maps. The external map is a `WeakHashMap` to allow logger-specific message factories to be GC-ed. Closes #3143.
Removes weak references to `Logger`s in `LoggerRepository`. The usage of weak references in `LoggerRepository` might cause `null` to be returned by `LogManager.getLogger()` of all Log4j Core versions up to `2.24.1`. Versions of Log4j API up to `2.24.0` did hold **hard** references to all the registered loggers, so the change will not alter the previous behavior. This PR also inverts the order of the `String` and `MessageFactory` keys to the `LoggerRepository` multi-map to limit the number of internal maps. The external map is a `WeakHashMap` to allow logger-specific message factories to be GC-ed. Closes apache#3143.
) This is a port of #3199 to the 2.24.x branch. Removes weak references to `Logger`s in `LoggerRepository`. The usage of weak references in `LoggerRepository` might cause `null` to be returned by `LogManager.getLogger()` of all Log4j Core versions up to `2.24.1`. Versions of Log4j API up to `2.24.0` did hold **hard** references to all the registered loggers, so the change will not alter the previous behavior. This PR also inverts the order of the `String` and `MessageFactory` keys to the `LoggerRepository` multi-map to limit the number of internal maps. The external map is a `WeakHashMap` to allow logger-specific message factories to be GC-ed. Closes #3143. Co-authored-by: Volkan Yazıcı <[email protected]>
) This is a port of #3199 to the 2.24.x branch. Removes weak references to `Logger`s in `LoggerRepository`. The usage of weak references in `LoggerRepository` might cause `null` to be returned by `LogManager.getLogger()` of all Log4j Core versions up to `2.24.1`. Versions of Log4j API up to `2.24.0` did hold **hard** references to all the registered loggers, so the change will not alter the previous behavior. This PR also inverts the order of the `String` and `MessageFactory` keys to the `LoggerRepository` multi-map to limit the number of internal maps. The external map is a `WeakHashMap` to allow logger-specific message factories to be GC-ed. Closes #3143. Co-authored-by: Volkan Yazıcı <[email protected]>
Removes weak references to
Logger
s inLoggerRepository
. The usage of weak references inLoggerRepository
might causenull
to be returned byLogManager.getLogger()
of all Log4j Core versions up to2.24.1
. Versions of Log4j API up to2.24.0
did hold hard references to all the registered loggers, so the change will not alter the previous behavior.This PR also inverts the order of the
String
andMessageFactory
keys to theLoggerRepository
multi-map to limit the number of internal maps. The external map is aWeakHashMap
to allow logger-specific message factories to be GC-ed.Closes #3143.