Course contentsShow
← Linux
Lesson 329 of 1,8193. The Filesystem Hierarchy and POSIX StandardsFree lesson

Filesystem Cache Eviction Policies

Evaluate how the kernel manages memory pressure by flushing cached filesystem data.

In this lesson, you will learn how the Linux kernel decides which cached filesystem data to discard when your system runs low on available RAM.

Understanding Memory Pressure

The Linux kernel operates on the principle that "unused RAM is wasted RAM." To speed up I/O, the kernel uses the Page Cache, keeping recently read file data in memory so it doesn't have to fetch it from the disk again. However, when you launch a heavy application and system memory starts to run low, the kernel faces memory pressure. It must find space for new data by "evicting" old data from the cache.

The LRU Strategy

The kernel primarily uses a Least Recently Used (LRU) policy to manage this cache. Imagine a deck of cards where every time you access a file, you move its corresponding "page" to the top of the stack. When the kernel needs to free up space, it looks at the bottom of the stack—the pages that haven't been touched in the longest time—and evicts them.

Flushing Dirty Pages

A crucial distinction exists between "clean" and "dirty" pages:

  • Clean pages: The data in the cache is identical to the data on the disk. The kernel can simply discard these without extra work.
  • Dirty pages: The data in the cache has been modified by the CPU but not yet written back to the physical disk.

Before the kernel can evict a dirty page, it must trigger a flush operation, writing that data back to the storage device to ensure filesystem consistency. You can manually suggest this to the kernel using the sync command or influence how aggressively the kernel flushes via /proc/sys/vm/dirty_background_ratio.

# Force the kernel to write all dirty buffers to disk
sync

If the system remains under high pressure, the kernel prioritizes flushing and evicting these pages to maintain stability. By constantly balancing the need for speed (keeping data in cache) with the reality of finite memory, the kernel keeps the system responsive without crashing when resources become scarce.

Key Takeaway: The Linux kernel uses a Least Recently Used (LRU) policy to manage the page cache, prioritizing the eviction of clean data and forcing "dirty" data to be written to disk to reclaim memory during times of high pressure.