Do logging impact application performance? First thing to know is , whether logs are written serially as processing takes place or are logged into file i a separate process? In java world how should I improve performance of my application where I need to log a lot in that case? Micro optimization is a side effect but I am looking for impact on putting log messages in java code. I later found , seems there is impact and log4j 2 seems originated out of this impact.
Well I see some possibilities about performances issues there, I will assume the logging framework you use is decent enough to not be the source of the problem.
- Synchronous logger : if you have configure synchronous logging everywhere, all your thread will regularly wait to be able to write at the disk. Synchronous logging is adequate for audit purpose where every log of something that has been done must figure.
- Asynchronous logging : it will take more memory and some more thread to handle. If your application interrupts, you will probably miss some of the last logs of the things that were done. So if you loaded your logs too much, it will either take lot of memory, of flush more often on the disk. Threads will only waits to be able to get the lock on the in memory buffer.
- Problem on development side : sometimes performance comes not from the configuration but from how the message to log is built (string concatenation, reinstantation of multiple object in lopo when not necessary,...), which take unecessary memory and cpu (and more garbage collection ? ).
To resume : with logging you may have trouble with :
- disk usage blocking processing while waiting for the possibility to write.
- memory consumption (and so eventually full GC)
- in-memory locks : when using asynchronous logging, if you log everything to one single file it is possible that your thread start to wait quite some time to access to the in-memory buffer storing all pending logs to write.
Depends on the logging framework you use.
It will have less performance impact, if the logging is done asynchronously, and sending the log messages is more or less a queueing operation in 1st place.
This might change the behavior regarding possible race conditions of your code though.
A bigger performance impact has to be considered, if logging is implemented synchronously and log messages are directly flushed to the log file.