当前位置:网站首页>NiO zero copy
NiO zero copy
2022-06-24 22:21:00 【Nice2cu_ Code】
Zero copy
List of articles
One 、 Tradition IO The problem of
The contents of the file in the server , adopt socket Of API, Send to client :
File f = new File("helloword/data.txt");
RandomAccessFile file = new RandomAccessFile(file, "r");
// Read data from file to byte Array
byte[] buf = new byte[(int)f.length()];
file.read(buf);
// Take data from byte Write array to network socket
Socket socket = ...;
socket.getOutputStream().write(buf);
The internal workflow of read-write operation is as follows :

java Not in itself IO Reading and writing ability , therefore read After method call , From you to java programmatic User mode Switch to Kernel mode , To call the reading ability of the operating system , Read data into Kernel buffer . During this time, the user thread blocks , Operating system usage DMA To realize the file reading operation ( Will not be used during CPU)
DMA It can be understood as hardware dedicated to data transmission , Will not take advantage of CPU Time
from Kernel mode Switch back to the User mode , Take data from Kernel buffer Read in User buffer ( namely
byte[] buf), During this time CPU Will participate in copying , Can't use DMAcall write Method , Take data from User buffer write in socket buffer ,CPU Will participate in copying
Next, write data to the network card , This ability java I don't have , So we have to start from User mode Switch to Kernel mode , Call the write ability of the operating system , Use DMA take socket buffer Write the data to the network card , Can't use CPU
You can see the tradition IO The problem is :
- User mode And Kernel mode The switch of... Happened 3 Time , This operation is relatively heavyweight
- The data is copied 4 Time
Two 、NIO Optimize
NIO Optimization is achieved by using Direct memory
DirectByteBuffercall
ByteBuffer.allocateDirect(10), Back toDirectByteBufferUsing operating system memory ,java Both the layer and the operating system can access- call
ByteBuffer.allocate(10), Back toHeapByteBufferIt uses java Memory
- call
The reading and writing process is as follows :

Most of the steps are the same as before optimization , I won't repeat .
Difference : Kernel buffer and user buffer , Share a piece of memory DirectByteBuffer , One less copy of data .
The switching times between user mode and kernel mode are not reduced .
1. Linux 2.1 Optimize ( Zero copy )
Adopted Linux 2.1 Provided later sendFile Method , stay java There are two corresponding methods that can be called to sendFile Method :
- channel call
transferTo/transferFromMethods copy data
The reading and writing process is as follows :

The first 2 In step , Kernel buffer The data in does not need to be written to array or direct memory , By calling sendFile Method writes data directly to socket buffer , Not pass java level ( Two less switches between kernel state and user state ).
Be careful : Data from Kernel buffer Transferred to the socket buffer ,CPU Will participate in copying .
There is only one switch between user mode and kernel mode , The data is copied 3 Time .
2. Linux 2.4 Optimize ( Zero copy )
The bottom layer is still used linux 2.1 Provided later sendFile Method .
The reading and writing process is as follows :

Kernel buffer The data can be written directly to the network card .
Only will Kernel buffer A small amount in offset and length Copy information into socket buffer , Almost no consumption .
There is only one switch between user mode and kernel mode in the whole process , The data is copied 2 Time .
3、 ... and 、 Zero copy
call linux 2.1 Provided later sendFile Copies of methods can be called Zero copy .
The so-called zero copy , There is no copy operation , It is Data will not be copied to Java In the memory , The advantages of zero copy are :
- Less switching between user state and kernel state
- Do not use CPU Calculation , Reduce CPU Time
- Zero copy is suitable for small file transfer
- If the file is large , Will read the data into the kernel buffer , And the purpose of the buffer ( advantage ) It is a loop to get a small amount of data
- Read a lot of data at once , You can't take advantage of buffers
- If the buffer memory is large , It will affect the use of buffer by other data
- If the file is large , Will read the data into the kernel buffer , And the purpose of the buffer ( advantage ) It is a loop to get a small amount of data
边栏推荐
- CV2 package guide times could not find a version that satisfies the requirement CV2 (from versions: none)
- leetcode:55. 跳跃游戏【经典贪心】
- Several schemes of traffic exposure in kubernetes cluster
- Servlet详解
- [untitled]
- 如何抓手机的包进行分析,Fiddler神器或许能帮到您!
- leetcode:515. Find the maximum value in each tree row [brainless BFS]
- TCP RTT measurement tips
- Flutter-使用 typedef的注意事项
- Double linked list implementation
猜你喜欢

first-order-model实现照片动起来(附工具代码) | 机器学习

【OpenCV 例程200篇】209. HSV 颜色空间的彩色图像分割

Opengauss kernel: simple query execution

leetcode-201_ 2021_ 10_ seventeen

Multithreaded finalization

Maximum flow problem

嵌入式开发:技巧和窍门——干净地从引导加载程序跳转到应用程序代码

Binary search tree template

Raspberry pie preliminary use

Docker 安装 MySQL 8.0,详细步骤
随机推荐
leetcode_ 191_ 2021-10-15
Excel layout
Filtered data analysis
SAP interface debug setting external breakpoints
直播软件app开发,左右自动滑动的轮播图广告
In the first year of L2, arbitrum nitro was upgraded to bring more compatible and efficient development experience
Valueerror: cannot take a larger sample than population when 'replace=false‘
LINQ query collection class introductory cases Wulin expert class
如何提取网页中的日期?
Notes on writing questions (18) -- binary tree: common ancestor problem
Shutter precautions for using typedef
How to refine permissions to buttons?
The leader of ERP software in printing industry
Introduction, installation and use of postman tool
[200 opencv routines] 209 Color image segmentation in HSV color space
“阿里健康”们的逻辑早就变了
树莓派初步使用
Process communication mode
Interviewer: you said you are proficient in redis. Have you seen the persistent configuration?
Machine learning: linear regression