The page cache: spare RAM that is not wasted
Video 043 of 140 in Virtualization Foundations, in the unit “Memory: RAM, addresses and virtual memory”. 4:02, with English and Hindi audio.
On a Linux server, a memory tool often shows almost no free RAM, and that is usually not a problem. The kernel fills spare RAM with copies of file data, so that repeated reads come from memory rather than from a slow disk. This article explains what the page cache is, how a cache hit and a cache miss differ, why the free figure can mislead, and how to read the available figure on an Ubuntu server.
What you will learn
- The page cache is spare RAM holding copies of file data.
- A cache hit serves a read from memory, with no disk trip.
- Judge memory by its available figure, not the free figure.
- The kernel gives cache space back as soon as programs need it.
Why Linux uses spare RAM to hold file data
Reading a disk takes far longer than reading RAM. Programs also read the same files again and again. If every read had to go to the disk, programs would wait a long time. Memory that holds nothing useful is wasted, so the kernel, which is the core of the operating system, uses the spare part of RAM to keep copies of file data. Keeping those bytes in RAM saves the trip to the disk.
What the page cache is and how it fills
The page cache is RAM that the kernel fills with copies of file data, as files are read and written. The first read loads the file from disk into the cache. Any repeat read of the same bytes comes straight from RAM. A write goes into the cache first, and the kernel sends it on to the disk at a time it chooses.
How a cache hit and a cache miss differ
- Cache hit: the bytes are already in RAM, so they come from RAM and the disk is not touched.
- Cache miss: the bytes are not in the cache, so the kernel reads them from disk, keeps a copy, and hands them over.
Files read again and again tend to be hits, so many reads avoid the disk. When a program needs the memory, the kernel gives the cache space back to it.
Why a low free figure can mislead you
Spare RAM fills with file data, so a memory tool can show little free memory. That looks alarming, but it is normal. The usual mistake is to read only the free figure. The memory may be in good shape.
The real danger comes when programs need more memory than is available. The cache shrinks, and reads go back to the slow disk. You notice it as available memory falling low, and disk reads rising while programs slow down.
How to read the memory figures on Ubuntu
On Ubuntu, the free command shows the memory situation. Judge it by the available column, not by the free column alone. The available column counts the memory that programs can still get, including the cache the kernel can give back.
free -hThe meminfo file in the proc folder has a MemAvailable line, which is the figure that programs can still get.
grep -E '^(Cached|MemAvailable)' /proc/meminfoTry it yourself
You need an Ubuntu machine. You will read the memory situation with the available column, then the MemAvailable line of the kernel's memory file.
Show the memory situation in readable units. Use the available column to judge memory, not the free column.
free -hWhat you should see: The memory figures in readable units. Read the available column, not the free column.
Show the Cached and MemAvailable lines of the meminfo file. MemAvailable is the figure that programs can still get.
grep -E '^(Cached|MemAvailable)' /proc/meminfoWhat you should see: The Cached and MemAvailable lines. MemAvailable is what programs can still get.
Terms
- Page cache
- Memory that the kernel uses to keep copies of file contents, so that repeated reads come from RAM.
- Cache hit
- A read whose bytes are already in the cache, so they come from RAM and the disk is not touched.
- Cache miss
- A read whose bytes are not in the cache, so the kernel must read them from disk first.
Check yourself
Why can a near-zero free memory figure be normal on Linux?
Spare RAM fills with copies of file data, and the kernel can give that memory back. Check the available figure instead.
What does the kernel do on a cache miss?
It reads the bytes from disk, keeps a copy in the cache, and hands the bytes to the program.
Which memory figure should you judge a server by?
The available figure, because it counts the memory that programs can still get.
Previous: Swap: when RAM overflows onto disk. Next: NUMA: memory that is near and far.