[ https://issues.apache.org/jira/browse/LOG4J2-2806?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17059264#comment-17059264 ]
Grzegorz Grzybek edited comment on LOG4J2-2806 at 3/14/20, 8:18 AM: -------------------------------------------------------------------- I admit my test was a bit artificial ;). And I'm changing Pax Logging anyway to use less WeakReferences assuming the number of loggers will be ~thousands, not ~millions. (I wouldn't be able to solve Logback case anyway and Log4J1 case is even more messy than with Log4J2). Here's the test: [Log4J2MemoryIntegrationTest.java|https://github.com/ops4j/org.ops4j.pax.logging/blob/master-serious-gc/pax-logging-it/src/test/java/org/ops4j/pax/logging/it/Log4J2MemoryIntegrationTest.java]. I've however thought more about this and you're right - no one should create so many loggers. I know how fragile LoggerRegistry and LoggerContexts are and I still think {{WeakMapFactory}} should not be used to create a map, where Logger (value) keeps strong reference to logger name, which is weak key of the map. But because no one can even configure such {{WeakMapFactory}} in LoggerRegistry, I'd resolve this issue as _won't fix_. was (Author: gzres): I admit my test was a bit artificial ;). Here's the test: [Log4J2MemoryIntegrationTest.java|https://github.com/ops4j/org.ops4j.pax.logging/blob/master-serious-gc/pax-logging-it/src/test/java/org/ops4j/pax/logging/it/Log4J2MemoryIntegrationTest.java]. I've however thought more about this and you're right - no one should create so many loggers. I know how fragile LoggerRegistry and LoggerContexts are and I still think {{WeakMapFactory}} should not be used to create a map, where Logger (value) keeps strong reference to logger name, which is weak key of the map. But because no one can even configure such {{WeakMapFactory}} in LoggerRegistry, I'd resolve this issue as _won't fix_. > Strange usage of WeakMapFactory in LoggerRegistry > ------------------------------------------------- > > Key: LOG4J2-2806 > URL: https://issues.apache.org/jira/browse/LOG4J2-2806 > Project: Log4j 2 > Issue Type: Task > Reporter: Grzegorz Grzybek > Priority: Major > > I'm working on https://ops4j1.jira.com/browse/PAXLOGGING-311 and I'm checking > some (maybe a bit artificial) scenario where I create millions of unique > loggers. > For this purpose, I've changed > {{org.apache.logging.log4j.core.LoggerContext#loggerRegistry}} from: > {code:java} > private final LoggerRegistry<Logger> loggerRegistry = new LoggerRegistry<>(); > {code} > to: > {code:java} > private final LoggerRegistry<Logger> loggerRegistry = new > LoggerRegistry<>(new LoggerRegistry.WeakMapFactory<>()); > {code} > The problem is that this didn't help. the _inner_ map (from logger name to > instance of {{org.apache.logging.log4j.core.Logger}}) was proper > {{WeakHashMap}}, but they weak key (logger name) is still strongly referenced > from the value itself. > {{WeakHashMap}} Javadoc says: > bq. The value objects in a WeakHashMap are held by ordinary strong > references. Thus care should be taken to ensure that value objects do not > strongly refer to their own keys, either directly or indirectly, since that > will prevent the keys from being discarded. > I know {{LoggerRegistry.WeakMapFactory}} is used by > {{org.apache.logging.log4j.taglib.Log4jTaglibLoggerContext}}, but does it > work? -- This message was sent by Atlassian Jira (v8.3.4#803005)