Understanding Zero-Copy in Java: NIO, MappedByteBuffer, DirectByteBuffer, and Netty

This article explains the concept of zero‑copy, its performance benefits, and how Java NIO, memory‑mapped buffers, direct buffers, channel‑to‑channel transfers, and Netty’s composite buffers implement zero‑copy to reduce data copying between kernel and user space.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding Zero-Copy in Java: NIO, MappedByteBuffer, DirectByteBuffer, and Netty

Zero‑copy refers to techniques that avoid unnecessary data copying between kernel and user space, dramatically improving system performance; it is widely used in Java NIO, Netty, Kafka, RocketMQ, and other high‑throughput frameworks.

The article first introduces basic I/O concepts such as buffers, the read/write flow, and explains how traditional read/write incurs multiple copies, while zero‑copy methods like mmap+write and sendfile eliminate these copies.

In Java, zero‑copy is realized through MappedByteBuffer . The FileChannel.map() method creates a virtual memory mapping between a file and a buffer, allowing direct access to file data without an intermediate copy. Example code demonstrates mapping a file, reading bytes, and printing them.

public class MappedByteBufferTest {
    public static void main(String[] args) throws Exception {
        File file = new File("D://db.txt");
        long len = file.length();
        byte[] ds = new byte[(int) len];
        MappedByteBuffer mappedByteBuffer = new FileInputStream(file)
            .getChannel().map(FileChannel.MapMode.READ_ONLY, 0, len);
        for (int offset = 0; offset < len; offset++) {
            byte b = mappedByteBuffer.get();
            ds[offset] = b;
        }
        Scanner scan = new Scanner(new ByteArrayInputStream(ds)).useDelimiter(" ");
        while (scan.hasNext()) {
            System.out.print(scan.next() + " ");
        }
    }
}

The map() method signature is

public abstract MappedByteBuffer map(MapMode mode, long position, long size) throws IOException

, where MapMode can be READ_ONLY, READ_WRITE, or PRIVATE.

Java also provides DirectByteBuffer , which allocates memory outside the JVM heap. It can be created via ByteBuffer.allocateDirect(100), giving a 100‑byte off‑heap buffer.

ByteBuffer directByteBuffer = ByteBuffer.allocateDirect(100);

For efficient file transfer, FileChannel.transferTo() moves data directly between channels without an intermediate user‑space buffer. The article includes a sample program that transfers a file to System.out using this method.

public class ChannelTransfer {
    public static void main(String[] argv) throws Exception {
        String[] files = new String[1];
        files[0] = "D://db.txt";
        catFiles(Channels.newChannel(System.out), files);
    }
    private static void catFiles(WritableByteChannel target, String[] files) throws Exception {
        for (int i = 0; i < files.length; i++) {
            FileInputStream fis = new FileInputStream(files[i]);
            FileChannel channel = fis.getChannel();
            channel.transferTo(0, channel.size(), target);
            channel.close();
            fis.close();
        }
    }
}

Netty implements zero‑copy with CompositeChannelBuffer , which aggregates multiple ChannelBuffer instances without copying their contents, and with Slice buffers for splitting. The source code shows how the buffer stores component references and calculates indices to access the underlying data directly.

public class CompositeChannelBuffer extends AbstractChannelBuffer {
    private final ByteOrder order;
    private ChannelBuffer[] components;
    private int[] indices;
    private int lastAccessedComponentId;
    private final boolean gathering;
    public byte getByte(int index) {
        int componentId = componentId(index);
        return components[componentId].getByte(index - indices[componentId]);
    }
    ...
}

Other systems such as RocketMQ and Kafka also adopt zero‑copy techniques (e.g., mmap+write in RocketMQ and sendfile in Kafka) to minimize data copying during message persistence and transmission.

In summary, zero‑copy works by sharing references to the same memory region across kernel and user space, ensuring that modifications are visible without creating duplicate copies, thereby achieving higher throughput and lower latency.

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.

JavaperformancenioNettyZero CopyMemory-Mapped I/O
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

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.