且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JAVA NIO 中的 zerocopy 技术提高IO性能

更新时间:2021-07-24 17:38:31

 关于一篇更详细更好的介绍 ZeroCopy技术的文章,可参考:JAVA IO 以及 NIO 理解

 

这篇文章介绍了 zerocopy技术来提高Linux平台上的IO密集型的JAVA应用程序的性能.

 

zerocopy技术能够避免中间缓冲区中的冗余数据复制以及减少Linux内核空间和用户空间上下文交换的次数。

适用场景:Many Web applications serve a significant amount of static content, which amounts to reading data off of a disk and writing the exact same data back to the response socket.

应用程序从本地磁盘读取数据,再将读取的数据原封不动地发送给Socket。

 

不采用zerocopy技术时的数据传输过程如下:

the kernel reads the data off of disk and pushes it across the kernel-user boundary to the application, and then the application pushes it back across the kernel-user boundary to be written out to the socket. In effect, the application serves as an inefficient intermediary that gets the data from the disk file to the socket.

数据先从本地磁盘读取到内核空间中,再通过缓冲区由用户程序得到,用户程序再通过缓冲区将数据发送到Socket。

 

JAVA NIO 中的 zerocopy 技术提高IO性能

 

Each time data traverses the user-kernel boundary, it must be copied, which consumes CPU cycles and memory bandwidth. Fortunately, you can eliminate these copies through a technique called — appropriately enough — zero copy.

每次数据传输到 内核-用户 缓冲区时,必须进行复制,这消耗了CPU和内存,而通过 zerocopy技术 可以去掉复制操作。

Applications that use zero copy request that the kernel copy the data directly from the disk file to the socket, without going through the application.

用户程序通过zerocopy请求使得数据直接从内核发送到Socket。

 

JAVA类库通过java.nio.channels.FileChanneltransferTo()方法支持zerocopy技术。

You can use the transferTo() method to transfer bytes directly from the channel on which it is invoked to another writable byte channel, without requiring data to flow through the application。

 

参考: https://www.ibm.com/developerworks/linux/library/j-zerocopy/


本文转自hapjin博客园博客,原文链接:http://www.cnblogs.com/hapjin/,如需转载请自行联系原作者