Skip to content

[logging] Enable Log4j2 Garbage Free Logging to reduce GC pressure - #8944

Merged
merlimat merged 1 commit into
apache:masterfrom
lhotari:lh-configure-gc-free-log4j2
Dec 14, 2020
Merged

merlimat merged 1 commit into
apache:masterfrom
lhotari:lh-configure-gc-free-log4j2

Conversation

@lhotari

@lhotari lhotari commented Dec 13, 2020 •

Copy link
Copy Markdown
Member

Motivation

Log4j2 has a Garbage Free logging mode which reduces GC pressure since the garbage created by logging is very low ("garbage free"). This mode is the default of Log4j2 unless there is javax.servlet.Servlet class available at runtime.

In Pulsar there is javax.servlet.Servlet class available at runtime and Log4j2's J2EE/JEE web application detection disables Log4j2 Garbage Free logging mode. Log4j2 Garbage Free logging mode doesn't support unloading & loading web applications which would be done in traditional J2EE application servers.

Since Pulsar isn't a J2EE/JEE server and doesn't need to unload & reload J2EE/JEE web applications, it's the correct thing to disable Log4j2's web application detection so that the more performant Log4j2 Garbage Free logging mode can be used.

The performance benefits of the Garbage Free logging mode is described in the Log4j2 documentation:
https://logging.apache.org/log4j/2.x/manual/garbagefree.html

Modifications

- Configure log4j2 to disable servlet webapp detection so that Garbage
  free logging can be used.

  - Log4j2 will disable Garbage free logging if there is javax.servlet.Servlet class
    available at runtime unless "-Dlog4j2.is.webapp=false" is passed
    on the command line.
    - See source code at
      https://github.com/apache/logging-log4j2/blob/master/log4j-api/src/main/java/org/apache/logging/log4j/util/Constants.java#L25-L41

  - Documented at https://logging.apache.org/log4j/2.x/manual/garbagefree.html
    "From version 2.6, Log4j runs in "garbage free" mode by default where objects
     and buffers are reused and no temporary objects are allocated as much as possible.
     There is also a "low garbage" mode which is not completely garbage free but does
     not use ThreadLocal fields. This is the default mode when Log4j detects it is
     running in a web application."
    ...
    "ThreadLocal fields holding non-JDK classes can cause memory leaks in web applications
     when the application server's thread pool continues to reference these fields after
     the web application is undeployed. To avoid causing memory leaks, Log4j will not use
     these ThreadLocals when it detects that it is used in a web application (when the
     javax.servlet.Servlet class is in the classpath, or when system property log4j2.isWebapp
     is set to "true")."
@eolivelli

Copy link
Copy Markdown
Contributor

Pulsar relays heavily on JAX-RS that is a server based technology.
Can you explain more why are you saying that we do not use Java Web Servet technologies?

I am not against this change but I would like to understand better

@lhotari

lhotari commented Dec 13, 2020 •

Copy link
Copy Markdown
Member Author

Can you explain more why are you saying that we do not use Java Web Servet technologies?

I didn't mean that. I reworded the sentence, it's now "Since Pulsar isn't a J2EE/JEE server and doesn't need to unload & reload J2EE/JEE web applications, it's the correct thing to disable Log4j2's web application detection so that the more performant Log4j2 Garbage Free logging mode can be used." (I also made other edits.)

Is that better now?

@eolivelli

Copy link
Copy Markdown
Contributor

what about log4j2.garbagefreeThreadContextMap ? probably it is not worth, and we should perform benchmarks

@sijie sijie added this to the 2.8.0 milestone Dec 14, 2020
@sijie sijie added the type/enhancement The enhancements for the existing features or docs. e.g. reduce memory usage of the delayed messages label Dec 14, 2020
@lhotari

lhotari commented Dec 14, 2020 •

Copy link
Copy Markdown
Member Author

what about log4j2.garbagefreeThreadContextMap ? probably it is not worth, and we should perform benchmarks

@eolivelli Good point. That would also be required for completely configuring Log4j2 Garbage Free Logging. (LOG4J2-1349 Garbage-free ThreadContext map) Do you think it should be part of this PR?

@eolivelli eolivelli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm
Let's do one step at a time

@merlimat
merlimat merged commit 13e57bd into apache:master Dec 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type/enhancement The enhancements for the existing features or docs. e.g. reduce memory usage of the delayed messages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants