Does log4j use NIO to write data to a file?

It seems to be pretty fast, and I'm just wondering if anyone knows about using NIO. I tried to find all the source code for NIO (well, its search method :) lol); but didn't hit anything. Also, if it does not use NIO; Do you find it appropriate to change log4j to use NIO and make it faster? Any pointers are advised and links to some resources will be very useful.

+4
source share
4 answers

In addition, if NIO is not used; do you think it's worth changing log4j to use NIO to make it faster?

Not if registration is not a significant part of your applications, in which case usually something is wrong.

You seem to have the impression that NIO is β€œfaster,” but overall it is not. Just try creating two files: one with standard IO and one with NIO, writing a bunch of data and closing it. You will see that performance is hardly different. NIO will only work in certain use cases; most often in the case of many connections.

+3
source

Check the source of FileAppender . Pretty simple standard java.io

+3
source

I see no reason why FileChannel could be faster than FileOutputStream in this case.

perhaps using a MappedByteBuffer? but in add mode, the behavior is OS dependent.

Ultimately, performance depends on your hard drive; your code is very small.

+1
source

When developing answers to confusion, they also block NIO files. That's why it is no faster than the traditional File IO in some scripts. Quoting the O'Reilly Java NIO book :

File channels are always blocked and cannot be placed in non-blocking mode. Modern operating systems have sophisticated caching and prefetching algorithms that usually give the local I / O drive a very low latency. Network file systems typically have higher latencies, but often benefit from the same optimization. The non-blocking paradigm of thread-oriented input-output does not make much sense for file-oriented due to the fundamentally different nature of input-output files. For file I / O, the true winner is asynchronous I / O, which allows you to process a request for one or more I / O operations from the operating system but does not wait for them to complete. The process is notified at a later time when the requested I / O is completed. Asynchronous I / O is an advanced feature not available on many operating systems. these are seen as future improvements to NIO.

Change With that said, you can improve read and write performance by using File NIO with MappedByteBuffer. Please note that the use of MappedByteBuffer in Log4j 2 is under consideration .

+1
source

Source: https://habr.com/ru/post/1314404/


All Articles