Fundamentals 15 min read

Understanding Zero‑Copy Techniques: DMA, sendfile, mmap and Direct I/O

This article explains the concept of zero‑copy I/O, how DMA offloads data movement between memory, disks and network cards, and compares practical implementations such as sendfile, mmap and Direct I/O, highlighting their benefits, limitations and typical use cases in Linux systems.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Understanding Zero‑Copy Techniques: DMA, sendfile, mmap and Direct I/O

Zero‑copy is a technique where the CPU does not have to copy data between memory regions; instead, data moves directly between devices or kernel buffers, reducing CPU work and context switches.

Four copies and four context switches occur in a naïve read‑write flow: from disk to page cache, page cache to socket buffer, kernel to user buffer, and user buffer back to kernel, each causing a user‑kernel transition.

Using DMA , a dedicated controller handles the data transfer between memory and I/O devices, allowing the CPU to issue only control commands. This eliminates two of the four copies and reduces context switches.

Zero‑copy implementations :

sendfile combines a read and write into a single system call, uses DMA and passes file descriptors to avoid copying data between kernel buffers.

mmap maps a file directly into user space, letting the application read/write the same pages without extra copies.

Direct I/O bypasses the page cache entirely, transferring data straight between user buffers and the device via DMA; it requires page pinning and may still need fsync for metadata.

Each method has trade‑offs: sendfile cannot be used when the data must be processed, mmap requires careful management of mapped regions, and Direct I/O adds complexity due to page pinning and lack of caching.

Typical use cases include high‑throughput message queues like Kafka, where producers write logs to disk using mmap and consumers send data over the network with sendfile, and databases that employ Direct I/O for self‑caching workloads.

The article concludes that Linux offers multiple zero‑copy strategies—reducing user‑kernel copies, bypassing the kernel, or optimizing buffer transfers—each suited to different performance requirements.

performanceLinuxDMAMMAPsendfileZero CopyDirect I/O
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.