|This article relies largely or entirely upon a single source. (December 2011)|
"Zero-copy" describes computer operations in which the CPU does not perform the task of copying data from one memory area to another. This is most often used to save on processing power and memory use when sending files over a network.
Zero-copy versions of operating system elements such as device drivers, file systems, and network protocol stacks greatly increase the performance of certain application programs and more efficiently utilize system resources. Performance is enhanced by allowing the CPU to move on to other tasks while data copies proceed in parallel in another part of the machine. Also, zero-copy operations reduce the number of time-consuming mode switches between user space and kernel space. System resources are utilized more efficiently since using a sophisticated CPU to perform extensive copy operations, which is a relatively simple task, is wasteful if other simpler system components can do the copying.
As an example, reading a file and then sending it over a network the traditional way requires four data copies and four CPU context switches, if the file is small enough to fit in the file cache. Two of those data copies use the CPU. Sending the same file via zero copy reduces the context switches to two, and eliminates either half, or all CPU data copies.
Zero-copy protocols are especially important for high-speed networks in which the capacity of a network link approaches or exceeds the CPU's processing capacity. In such a case the CPU spends nearly all of its time copying transferred data, and thus becomes a bottleneck which limits the communication rate to below the link's capacity. A rule of thumb used in the industry is that roughly one CPU clock cycle is needed to process one bit of incoming data.
Techniques for creating zero-copy software include the use of DMA-based copying and memory-mapping through an MMU. These features require specific hardware support and usually involve particular memory alignment requirements.
Several operating systems support zero-copying of files through specific APIs.
Linux supports zero copy through system calls such as sys/socket.h's sendfile, sendfile64, and splice.
Windows supports zero copy through the TransmitFile API.
Java input streams can support zero copy through the java.nio.channels.FileChannel's transferTo() method if the underlying operating system also supports zero copy.
RDMA (Remote Direct Memory Access) protocols deeply rely on zero-copy techniques.