Backend Development 11 min read

Understanding Zero‑Copy in Java: I/O Concepts, mmap, Sendfile, and Netty

This article explains the zero‑copy technique in Java, covering basic I/O concepts, buffer management, mmap + write and Sendfile methods, and how frameworks like NIO, Netty, Kafka, and RocketMQ use these mechanisms to eliminate data copies and improve performance.

Architect
Architect
Architect
Understanding Zero‑Copy in Java: I/O Concepts, mmap, Sendfile, and Netty

Zero‑copy means data is transferred without copying between user space and kernel space, greatly improving performance. It is widely used in Java frameworks such as NIO, Netty, Kafka, and RocketMQ.

We first review basic I/O concepts: buffers, kernel read/write cycles, and the inefficiency of copying data from kernel buffers to user buffers.

Two main zero‑copy techniques are mmap + write and the Sendfile system call. mmap maps a file directly into the process address space, eliminating the kernel‑to‑user copy, while Sendfile transfers data entirely within kernel space, avoiding user‑space buffers.

Java provides zero‑copy via FileChannel.map() returning a MappedByteBuffer , which can be used to read files without extra copies. Example:

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(FileChannel.MapMode mode, long position, long size) throws IOException;

Its parameters are MapMode (READ_ONLY, READ_WRITE, PRIVATE), position (start offset), and size (number of bytes).

Direct buffers allocate off‑heap memory, e.g.:

ByteBuffer directByteBuffer = ByteBuffer.allocateDirect(100);

Channel‑to‑channel transfer is possible with FileChannel.transferTo() :

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 using CompositeChannelBuffer and slice buffers, allowing aggregation of multiple buffers without copying. A snippet of the class:

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 use zero‑copy (mmap + write or Sendfile) for message storage and transmission.

In summary, zero‑copy in Java relies on memory‑mapped files, direct buffers, and kernel‑level transfer APIs to minimize data copies and context switches, thereby enhancing throughput.

JavaperformanceNIONettyMMAPsendfileZero Copy
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

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.