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

 關於一篇更詳細更好的介紹 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。

 

 

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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章