Understanding Zero‑Copy Technology: Concepts, Mechanisms, and Linux Implementations
Zero‑copy is an optimization technique that eliminates unnecessary memory copies between kernel and user space by leveraging DMA, memory‑mapping and specialized system calls, thereby reducing CPU load, latency and improving throughput for high‑performance networking, storage and multimedia workloads.
Zero‑copy is a data‑transfer optimization that minimizes the number of memory copies between kernel and user space, reducing CPU load and improving overall system performance. Traditional data transfer involves multiple copies: disk → kernel buffer → user‑space buffer, each incurring CPU cycles and system‑call overhead.
The technique bypasses these copies by using file descriptors, Direct Memory Access (DMA) and memory‑mapping, allowing data to move directly from disk or network to the memory region used by the application. This yields lower latency and higher throughput, especially in high‑speed network and large‑scale storage scenarios.
Basic concept : Zero‑copy aims for “CPU‑zero participation” by avoiding data movement between kernel and user buffers. It relies mainly on DMA and memory‑region mapping. In Linux, mechanisms such as mmap , sendfile , splice and FileChannel.transferTo implement zero‑copy.
Zero‑copy in Netty : Netty uses direct (off‑heap) ByteBuffer s, composite buffers to avoid extra copies, and the transferTo method for file transmission, all of which embody zero‑copy principles.
Traditional I/O example (Java) :
Socket socket = new Socket(HOST, PORT);
InputStream inputStream = new FileInputStream(FILE_PATH);
OutputStream outputStream = new DataOutputStream(socket.getOutputStream());
byte[] buffer = new byte[4096];
while (inputStream.read(buffer) >= 0) {
outputStream.write(buffer);
}
outputStream.close();
socket.close();
inputStream.close();This code illustrates the classic read‑write loop that incurs multiple kernel‑user transitions and CPU copies.
Data‑copy process analysis : Reading a file involves two context switches, one DMA copy (disk → kernel buffer) and one CPU copy (kernel buffer → user buffer). Writing follows a similar pattern. Consequently, traditional I/O suffers from redundant copies and state switches.
Zero‑copy mechanisms :
mmap : Maps kernel buffers into user space, eliminating one copy but still requiring a CPU copy inside the kernel.
sendfile : Transfers data directly from a file descriptor to a socket without passing through user space, saving two context switches.
splice : Creates a pipe between kernel and socket buffers, avoiding CPU copies.
FileChannel.transferTo (Java NIO): Directly moves bytes from a file channel to a socket channel.
Example using FileChannel.transferTo :
SocketAddress socketAddress = new InetSocketAddress(HOST, PORT);
SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(socketAddress);
File file = new File(FILE_PATH);
FileChannel fileChannel = new FileInputStream(file).getChannel();
fileChannel.transferTo(0, file.length(), socketChannel);
fileChannel.close();
socketChannel.close();Kafka and other high‑performance frameworks (e.g., Netty) adopt zero‑copy to achieve efficient data pipelines. Kafka’s transport layer delegates to FileChannel.transferTo to move log data to sockets without extra copies.
Relevant source excerpt from Kafka’s transport layer:
/**
* Transfers bytes from `fileChannel` to this `TransportLayer`.
* ...
*/
long transferFrom(FileChannel fileChannel, long position, long count) throws IOException;Implementation in PlaintextTransportLayer :
@Override
public long transferFrom(FileChannel fileChannel, long position, long count) throws IOException {
return fileChannel.transferTo(position, count, socketChannel);
}Overall, zero‑copy reduces redundant memory copies and context switches, freeing CPU cycles for other work and significantly improving throughput for high‑speed networking, storage, and multimedia applications.
Deepin Linux
Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.