Fundamentals 8 min read

Why Files Open Instantly After First Load: Inside Linux PageCache

PageCache, a kernel-managed memory cache, bridges the speed gap between slow storage and fast RAM, enabling rapid subsequent file accesses, accelerating copy‑paste operations, and optimizing read/write performance; this article explains its principles, mechanisms, and demonstrates its impact with Linux experiments.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Why Files Open Instantly After First Load: Inside Linux PageCache

Introduction

When you open a large PPT file for the first time it may take several seconds, but the second opening is almost instantaneous. The same phenomenon occurs when copying large files: the progress bar moves slowly at first and then speeds up. These behaviors are caused by a key operating‑system feature called PageCache , which caches disk data in RAM.

Storage Performance Gap

Mechanical hard drives and RAM differ by orders of magnitude in read/write speed—often a hundred‑fold difference. Directly reading from disk therefore hurts responsiveness, so the OS introduces PageCache to hide this gap.

What Is PageCache?

PageCache consists of memory pages that mirror physical blocks on disk. While most pages hold process data, the remaining free RAM is repurposed as PageCache. Its size is dynamic: it grows to use any idle memory and can shrink when memory pressure rises.

Unlike CPU L1/L2/L3 caches, which are hardware‑implemented, PageCache is a software cache managed by the kernel.

File Read Path

When a process issues a read system call, the kernel first checks whether the requested data resides in PageCache. If it does, the kernel returns the data directly from RAM (a cache hit). If not (a cache miss), the kernel performs a disk I/O operation, loads the data into PageCache, and then serves the request.

Only the pages that are actually accessed are cached; for a file with four pages, the kernel may cache just the first page if that is the one frequently used.

File Write Path

Writes typically follow a write‑through strategy: the kernel writes data to PageCache first and marks the pages dirty. The actual disk write happens asynchronously later, so the write appears as a fast, pure‑memory operation.

During a large file copy, the initial phase loads data from disk into PageCache, which limits speed to the disk’s sequential read rate. Once the data is fully cached, subsequent reads are served from RAM, causing the progress bar to accelerate dramatically.

Additional Optimizations

Read‑ahead : the kernel predicts and pre‑loads upcoming blocks to improve sequential read performance.

LRU replacement : PageCache uses a Least‑Recently‑Used algorithm to evict pages when memory is needed for other purposes.

Typical Use Cases

PageCache provides noticeable speed gains in scenarios with repeated file access, such as compiling code, reading configuration files, video editing, or processing database logs.

Experimental Verification on Linux

1. Create a 1 GB file filled with random data: dd if=/dev/urandom of=testfile bs=1M count=1024 2. Drop all existing caches: sync && echo 1 > /proc/sys/vm/drop_caches 3. Measure the time to read the file (cache miss): time cat testfile > /dev/null Typical output:

real    0m6.176s
user    0m0.028s
sys     0m0.731s

4. Read the file again (cache hit): time cat testfile > /dev/null Typical output:

real    0m0.309s
user    0m0.011s
sys     0m0.298s

The second read is roughly twenty times faster because it accesses data entirely from PageCache.

Conclusion

PageCache dramatically improves I/O performance by keeping frequently accessed disk data in RAM, turning many read and write operations into fast memory accesses. Understanding its behavior helps explain why the first access to a large file is slow while subsequent accesses are near‑instantaneous.

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.

file I/OPageCache
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.