NOTE
4.5 Zero-Copy I/O
Why zero-copy techniques reduce CPU-mediated data movement, with mmap, sendfile, page cache, DMA, and the limits of the term 'zero copy'.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Traditional File-to-Socket Transfer
A simple application may:
- read file data into a user-space buffer;
- write that buffer to a socket.
At a high level this can involve storage DMA into kernel/page-cache memory, a CPU-mediated copy to user space, another copy into socket/kernel buffers, then DMA to the network device.
Exact copy counts vary with kernel version, device capabilities, buffering, and protocol path, so fixed diagrams should be treated as explanatory models rather than universal laws.
2. What “Zero Copy” Means
Zero-copy techniques try to avoid unnecessary CPU-mediated copies between memory buffers, especially crossings into and back out of user space.
The phrase does not necessarily mean that literally no bytes move anywhere. DMA engines, NICs, storage devices, page mappings, and descriptor metadata can still be involved.
3. mmap
mmap maps file-backed pages into a process address space. The application can access cached file pages through virtual-memory mappings instead of explicitly copying them through read into a separate application buffer.
This is valuable when the application must inspect or modify the data, but mapping has page-fault, lifetime, and random-access trade-offs.
4. sendfile
sendfile transfers data between file and socket descriptors inside the kernel path, avoiding the ordinary user-space read/write bounce buffer.
On supported paths, the kernel can pass references/descriptors to existing page-cache data toward the networking stack and let DMA-capable hardware perform much of the movement.
5. Other Modern Mechanisms
Related techniques include:
splice/vmspliceon Linux;- scatter/gather I/O;
- direct I/O for workloads that deliberately bypass the page cache;
- registered/fixed buffers in newer asynchronous APIs.
6. Java / Netty Examples
Java NIO exposes mechanisms that can map efficiently to OS zero-copy facilities:
FileChannel.map→ memory mapping;FileChannel.transferTo/transferFrom→ may use kernel transfer primitives such assendfilewhere supported;- Netty
FileRegioncan use these facilities for file transfer.
Direct buffers reduce some copies in certain native-I/O paths, but “off-heap” by itself does not automatically mean zero-copy.