Fundamentals 80 min read

How Linux Kernel Handles JDK NIO File I/O: From Page Cache to Direct IO

This article deeply explores the Linux kernel's handling of JDK NIO file reads and writes, detailing the role of page cache, radix trees, buffered versus direct I/O, prefetch algorithms, dirty‑page write‑back mechanisms, and the key sysctl parameters that control performance and data safety.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
How Linux Kernel Handles JDK NIO File I/O: From Page Cache to Direct IO

Overview

The article explains how JDK NIO interacts with the Linux kernel for file I/O, covering both Buffered I/O and Direct I/O paths.

Buffered I/O Flow

Data is copied from a JVM HeapByteBuffer to a temporary DirectByteBuffer, then the write system call transfers it to the kernel where vfs_write writes into the page cache. The kernel marks the affected pages as dirty and later flushes them to disk.

Direct I/O Flow

When opened with O_DIRECT, JDK NIO bypasses the page cache; data is copied only from the DirectByteBuffer to kernel space and then DMA writes directly to the disk, reducing copy overhead.

Page Cache Internals

The kernel stores cached file pages in a struct address_space which uses a radix tree ( struct radix_tree_node) for fast lookup. Each node contains 64 pointers, allowing efficient mapping of page indices to struct page objects.

Pre‑fetch (Read‑Ahead)

Linux predicts sequential reads and pre‑loads pages into the page cache using a sliding “current window” and “ahead window”. The algorithm adjusts window size based on access patterns, and can be influenced by posix_fadvise flags such as POSIX_FADV_SEQUENTIAL or POSIX_FADV_RANDOM.

Dirty‑Page Write‑Back

Dirty pages are written back to disk either asynchronously by a background flusher thread or synchronously when thresholds are exceeded. Six sysctl parameters control this behavior: dirty_background_bytes / dirty_background_ratio – trigger asynchronous write‑back. dirty_bytes / dirty_ratio – trigger synchronous write‑back. dirty_expire_centisecs – maximum time a dirty page may stay in memory. dirty_writeback_centisecs – interval for the periodic flusher wake‑up.

Configuration

These parameters can be set temporarily via /proc/sys/vm/*, with sysctl, or permanently in /etc/sysctl.conf. Adjusting them balances data safety against I/O performance.

Conclusion

The article provides a complete, kernel‑level view of JDK NIO file operations, illustrating how page cache, radix trees, prefetch, and write‑back mechanisms work together, and how system tunables influence overall I/O efficiency.

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.

JavanioLinux kernelpage cacheI/O performance
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.