Linux Internals
The Linux Page Cache: Why Free Memory Looks Low
By NileVPS Team · Published · 4 min read
اقرأ المقال بالعربيA common worry after logging in to a new VPS: free -h says only a few hundred megabytes are free, even though your application uses little memory. Nothing is wrong. Linux is using spare RAM as a page cache, a copy of recently read and written file data kept in memory so the next access does not have to touch the disk. Understanding the page cache helps you read memory statistics correctly and size your server sensibly.
What the page cache is
Reading from RAM is orders of magnitude faster than reading from even the fastest NVMe drive. So whenever Linux reads a file block from disk, it keeps a copy in memory, organized in pages (usually 4 KB). The next read of the same data is served straight from RAM.
The same cache handles writes. A write() updates the page in memory and marks it dirty; the kernel writes it to disk later. That process is covered in our article on dirty pages and writeback.
Key properties:
- The cache is shared. If two processes read the same file, there is one copy in memory.
- The cache is reclaimable. When applications need memory, the kernel drops clean cached pages (and writes dirty ones first) to make room.
- Executables and shared libraries are cached too, which is why a program starts faster the second time.
- Memory-mapped files (
mmap) map page cache pages directly into a process.
Reading free correctly
free -h total used free shared buff/cache available
Mem: 3.8Gi 900Mi 250Mi 40Mi 2.7Gi 2.6Gi
Swap: 1.0Gi 0B 1.0Gi- free: memory nobody is using at all. On a healthy server this is often small.
- buff/cache: page cache plus kernel buffers and some reclaimable kernel memory.
- available: the kernel's estimate of how much memory new applications can get without swapping. This is the number to watch.
In the example, only 250 MB is "free", but 2.6 GB is available because most of the cache can be reclaimed. A server is genuinely short on memory when available stays low and swap usage or OOM-killer events appear.
For more detail:
grep -E 'MemAvailable|^Cached|Buffers|Active\(file\)|Inactive\(file\)' /proc/meminfoHow the kernel decides what to keep
Linux tracks file pages on active and inactive lists (newer kernels can use the multi-generational LRU). Pages accessed repeatedly move to the active list; pages that are not touched again age out and are reclaimed first. This protects frequently used data, such as a database's hot tables, from being evicted by a one-time read of a large backup file.
The vm.swappiness setting influences whether the kernel prefers to reclaim cache or swap out anonymous memory (application heap). Lower values favor keeping application memory in RAM and reclaiming cache first.
Seeing what is in the cache
Some useful tools:
# Cache usage over time
vmstat 2 # the 'cache' column
# Which files are cached (install from your distribution or build from source)
vmtouch -v /var/lib/mysql/
fincore /var/log/syslog # from util-linux on recent distributionsfincore and vmtouch show how many pages of a file are resident, which helps you check whether a database's data files fit in memory.
Dropping the cache: when and when not
You can ask the kernel to drop clean caches:
sync
echo 1 | sudo tee /proc/sys/vm/drop_caches # page cache only
echo 3 | sudo tee /proc/sys/vm/drop_caches # page cache plus dentries and inodesThis is useful for benchmarking (to measure cold-cache disk performance) or debugging. It is not a performance fix. After dropping caches, everything has to be read from disk again and the server is slower until the cache warms up. Adding drop_caches to cron is a common mistake that only hurts performance.
Page cache and server sizing
The page cache is why extra RAM often speeds up a server even when applications do not "use" it:
| Workload | Why cache matters |
|---|---|
| Databases (PostgreSQL) | PostgreSQL relies on the OS page cache in addition to its own shared buffers |
| Websites and static files | Frequently served files stay in memory |
| Build servers and CI | Source trees and dependencies are read repeatedly |
| Log-heavy services | Recent logs stay hot for tools like grep and tail |
A practical approach: watch the available value and disk read activity (iostat -x 1 or vmstat 2, column bi). If reads from disk stay high while your working data set could fit in RAM, a plan with more memory is likely to help more than a faster CPU. You can compare memory sizes on our pricing page.
Some software deliberately bypasses the page cache with direct I/O (the O_DIRECT flag) because it manages its own cache in user space.
Checklist
- Judge memory by
available, notfree. - Treat high
buff/cacheas normal and healthy. - Do not schedule
drop_caches. - Watch disk reads to see whether your data fits in cache.
- Add RAM when the working set does not fit and disk reads dominate.
FAQ
Is high buff/cache usage a memory leak?
No. Cached file data is released automatically when applications need memory. A leak shows up as growing used memory belonging to a process, falling available memory, and eventually swap or OOM-killer activity.
Should I clear the page cache regularly to free RAM?
No. Clearing it forces data to be read from disk again and makes the server slower. Use drop_caches only for benchmarks or troubleshooting.
Can I limit how much memory the page cache uses?
There is no simple global cap, and usually you should not set one. For specific services, cgroup v2 memory limits (for example MemoryMax= in a systemd unit) also cover the page cache charged to that service.