Optimize Performance of I/O-intensive Java applications Using Zero Copy
- 1. Zero Copy: A Green Approach
to Data Transfer
Pramod B Nagaraja
IBM India Pvt LTD
- 3. What is Zero Copy ?
•
Data transfer from disk to user buffers without
an intermediate copy into OS Kernel buffers
•
Computer operations in which CPU does not
perform the task of copying data from one
memory area to another
• DMA
• MMU
- 4. What is Zero Copy ?
•
Fewer intermediate Data Copies, fewer
context switches
•
Linux :: sendfile, sendfile64
- 7. Data Transfer : Age-Old Practice
File.read(fd, buf, len);
Socket.send(socket, buf, len);
inputStream = new FileInputStream(fname);
output = new
DataOutputStream(socket.getOutputStream());
byte[] b = new byte[4096];
long read = 0, total = 0;
while((read = inputStream.read(b))>=0) {
total = total + read;
output.write(b);
}
- 11. Data Transfer : Best Practice
FileChannel Approach
java.nio.channels.FileChannel.transferTo(pos, cnt, writableChannel);
SocketChannel sc = SocketChannel.open();
sc.connect(sad);
FileChannel fc = new
FileInputStream(fname).getChannel();
long nsent = 0, curnset = 0;
curnset = fc.transferTo(0, fsize, sc);
- 15. Performance Comparison
• ~ 65% Performance Boost up !
0
2000
4000
6000
8000
10000
12000
14000
16000
18000
63
M B
350
M B
1
GB
Traditio
nal
FileCha
nnel
Measurements take on Linux (xi32) with 2.6 kernel
- 20. Limitations
Suitable only for static content
Not suitable if Data to be transferred needs to
be modified/appended with/appended to, etc
FileChannels can only be used to transfer
data from File to File, File to Socket objects.
- 21. References
More details can be found @
http://www.ibm.com/developerworks/library/j-
zerocopy/index.html
http://www.linuxjournal.com/article/6345