The OS page cache is a portion of main memory that the kernel uses to store recently accessed file data. When a process reads or writes a file (whether through traditional system calls or Memory Mapping), the kernel keeps copies of the accessed blocks in the page cache. Subsequent operations on the same areas can then be served from RAM without hitting the disk, which greatly improves performance.
How It Works
When an application performs a read (e.g., read() syscall) on a file:
- The kernel checks if the required blocks are in the page cache.
- If they are, it can deliver the data directly from RAM (cache hit).
- If they are not, it reads those blocks from disk into the cache, then copies them to the application.
A similar mechanism applies to Memory Mapping a file. If the process accesses a page that is not yet cached, the OS loads it into a cache-backed page of physical memory and updates the process’s virtual mapping.
Dirty Pages and Writeback
When a process writes to a file via standard file I/O or an mmaped region, the kernel marks those pages as dirty in the cache. The OS periodically flushes or writes back these dirty pages to permanent storage in the background. This deferred write improves performance by batching disk operations, but it also means recent writes might not immediately appear on disk.
Interaction with User Space
- Normal file I/O (
read,write) and mmap-based I/O both leverage the page cache by default. - Direct I/O bypasses the page cache, letting applications manage their own buffering. This is sometimes used in database systems to avoid double caching of large datasets.
Allocations and Reclaim
Since the page cache is just ordinary RAM managed by the kernel, it can grow or shrink based on overall system activity:
- If user processes or other kernel subsystems need memory, the kernel may evict (reclaim) cached pages that are unused or clean.
- If a page is dirty and must be reclaimed, the kernel writes it to disk first (or discards it if it’s file-backed and unchanged from what’s on disk).
Through this mechanism, the page cache dynamically adapts to system load, caching what’s recently accessed while ensuring memory remains available for other demands.