Understanding Zero‑Copy in Java: I/O Concepts, mmap, sendfile, Netty and More
This article explains the principle of zero‑copy, its role in improving I/O performance, and demonstrates how Java NIO, mmap, sendfile, channel‑to‑channel transfer, Netty composite buffers and other techniques achieve zero‑copy in backend systems.
Zero‑copy means data can be transferred without the usual back‑and‑forth copying between user space and kernel space, dramatically improving system performance. It is widely used in frameworks such as Java NIO, Netty, Kafka and RocketMQ.
I/O Concepts – All I/O operations move data into or out of buffers; a process issues a read or write request to the OS, which either fills a user buffer from kernel data or copies kernel data to the user buffer.
Zero‑copy mechanisms – Two main approaches are mmap+write and sendfile . mmap+write maps a file directly into the process address space, eliminating the copy from kernel read buffer to user buffer. sendfile transfers data entirely within kernel space, avoiding a user‑space copy and reducing context switches.
Virtual Memory – Modern OSes use virtual memory, allowing multiple virtual addresses to refer to the same physical memory and enabling the kernel buffer to be mapped into user space so DMA can write directly to a buffer visible to both kernel and user processes.
Java NIO MappedByteBuffer
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;Parameters:
MapMode : READ_ONLY, READ_WRITE, PRIVATE.
Position : start offset in the file.
Size : number of bytes to map.
Channel‑to‑Channel Transfer
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();
}
}
}Its transferTo signature:
public abstract long transferTo(long position, long count, WritableByteChannel target) throws IOException;This method moves data directly between two kernel buffers without an intermediate user‑space buffer.
Netty Zero‑Copy – Netty provides composite and slice buffers to avoid copying when assembling or splitting messages. A simplified excerpt of CompositeChannelBuffer shows how it stores references to component buffers and accesses data without allocating new memory:
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 employ zero‑copy (e.g., mmap+write, sendfile) to serve messages efficiently.
In summary, zero‑copy in Java leverages OS virtual memory, memory‑mapped files, direct channel transfers, and framework‑level buffers to eliminate unnecessary data copies, thereby improving throughput and reducing latency in backend applications.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.