Fundamentals 15 min read

How Zero-Copy I/O Boosts Performance: From Theory to Java NIO

This article explains the traditional I/O read/write workflow, identifies its performance bottlenecks, introduces the concept of zero‑copy, and demonstrates practical zero‑copy implementations—including DMA, sendfile, shared memory, memory‑mapped files, and Java NIO techniques such as ByteBuffer, Channel, transferTo/transferFrom, and memory‑mapped files.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
How Zero-Copy I/O Boosts Performance: From Theory to Java NIO

Traditional I/O Read/Write Process

In a typical system, an application initiates a read or write request, the kernel copies data between the disk (or network) and a kernel buffer, then copies it again to a user buffer before the application can process it.

Simplified Read Flow

Application issues a read request.

Kernel reads data from disk or external storage into the kernel buffer.

Kernel copies data from the kernel buffer to the user buffer.

Application processes the data from the user buffer.

Detailed Read/Write Flow

The diagram shows separate sections for read (red) and write (blue) operations.

Key Concepts

Application : Programs running on the OS.

Kernel : Core OS components managing CPU, memory, processes, file systems, device drivers, and system calls.

External Storage : Hard drives, USB drives, etc.

Kernel Mode : Privileged execution mode with full hardware access.

User Mode : Restricted mode where applications run.

Mode Switch : Transition between user and kernel modes for system calls.

Kernel Buffer : Memory area used by the kernel to exchange data with external devices.

User Buffer : Memory area accessible directly by the application.

Disk Buffer : Temporary cache in RAM for data being read from or written to disk.

PageCache : Linux kernel cache that stores file data in memory to accelerate subsequent reads and writes.

Performance Bottlenecks

Traditional I/O suffers from multiple data copies (user ↔ kernel buffers) and frequent user‑kernel mode switches, which consume CPU cycles and memory bandwidth, especially under high concurrency.

What Is Zero‑Copy?

Zero‑copy aims to minimize the number of data copies and mode switches, ideally moving data directly between the source and destination without intermediate copies.

Zero‑Copy Techniques

Direct Memory Access (DMA)

DMA allows peripherals (e.g., network cards, disk controllers) to read/write memory directly, bypassing the CPU.

sendfile System Call

On Linux, sendfile transfers data from a file descriptor to a socket (or another file) without copying data to user space.

Shared Memory

Applications and the kernel share a common memory region, eliminating copies between user and kernel buffers.

Memory‑Mapped Files

Files are mapped into the process address space, allowing direct memory access without explicit read/write calls.

Zero‑Copy in Java

Standard Java I/O follows the traditional model, but Java NIO provides classes such as ByteBuffer and Channel that enable zero‑copy techniques. public static void ioCopy() { ... } The above method uses classic I/O and takes about 1.29 seconds to copy a ~110 MB PDF.

FileChannel.transferTo() and transferFrom()

These methods use the underlying OS sendfile when available. public static void nioTransferTo() { ... } Execution time: 0.536 seconds (≈2× faster). public static void nioTransferFrom() { ... } Execution time: 0.603 seconds.

Memory‑Mapped Files in Java

public static void nioMap() { ... }

Execution time: 0.663 seconds.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceI/ODMAsendfileZero CopyJava NIOmemory-mapped
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

0 followers
Reader feedback

How this landed with the community

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.