Memory, page cache and swap
Read free and /proc/meminfo correctly, see why Linux fills RAM with page cache on purpose, and decide how much swap, swappiness and zram you want.
Memory, page cache and swap
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Linux uses spare RAM as page cache, a copy of file data it has read or will write. Unused RAM does nothing, so a low `free` number on a busy server is normal.
- Read the `available` column of `free`, from MemAvailable in /proc/meminfo. It estimates how much memory new work can get without swapping, counting reclaimable cache.
- `used` in current procps is total minus available. buff/cache is the page cache and reclaimable kernel caches, mostly given back on demand.
- Clean cache pages can be dropped at any time. Dirty pages must be written back first, and anonymous memory (heap, stack) can only go to swap.
- Swap lets the kernel move rarely used anonymous pages out and keep useful cache in. A little swap in steady use is fine. Constant swap-in and swap-out is the problem.
- vm.swappiness, 0 to 200 with a default of 60, sets the relative cost of swapping versus dropping file cache. It is not a percentage of RAM in use.
- zram is a compressed swap device in RAM. Fedora has enabled swap on zram by default since Fedora 33.
- Writing to drop_caches is for testing. The kernel docs say not to use it outside testing or debugging.
“The server is out of memory”
Someone runs free on a database server with 64 GB of RAM and sees about 1 GB under free. A ticket is opened. Someone else adds a cron job that writes to drop_caches every hour to “free memory”, and queries get slower.
Both reactions come from reading Linux memory like an app’s memory. The kernel’s view is different: RAM that holds nothing is wasted, so it fills spare RAM with copies of files and gives that memory back the moment something needs it. Once you see that, free stops being alarming and the real warning signs become clear. The CS lesson on virtual memory has the paging theory. This is what Linux shows you.
Two kinds of memory
Almost every page of RAM in use is one of two kinds.
File-backed, the page cache. When a program reads a file, the kernel keeps a copy of those pages in RAM. The next read of the same data is a memory copy instead of a disk read. Writes go into the cache too and are written to disk later. A clean cached page, unchanged since it was read, can be dropped instantly because the file still has it. A dirty page, written but not yet on disk, has to be written back first.
Anonymous: heap, stack, anything a program allocates that has no file behind it. It exists nowhere else, so the kernel cannot drop it. It can only move it to swap.
A good analogy is a desk. File-backed pages are photocopies of library books: if the desk is full, throw one away and fetch it again later. Anonymous pages are your own handwritten notes: you can move them to a filing cabinet across the room, which is swap, but you cannot throw them away.
Reading free
free(1) from procps-ng parses /proc/meminfo and shows six columns:
total: usable RAM.used: per the current man page, calculated as total minus available.free: RAM holding nothing at all. Usually small, and that is fine.shared: mostly tmpfs, which lives in RAM.buff/cache: page cache plus reclaimable kernel slabs.available: fromMemAvailablein/proc/meminfo, available since Linux 3.14, an estimate of how much memory is available for starting new applications without swapping.
available is the number to watch and alert on. It counts free memory plus the cache that can actually be reclaimed. free alone is not a useful alert, because a healthy machine runs it low on purpose.
The used formula has changed over procps versions, so older guides that compute “used minus buffers minus cache” by hand are describing an older free. MemAvailable already does that work, and does it better.
/proc/meminfo, the fields that matter
free is a summary. proc_meminfo(5) documents the source. A few fields answer most questions:
| Field | Meaning |
|---|---|
MemAvailable |
The estimate above |
Cached |
The page cache, not counting swap cache |
Dirty, Writeback |
Waiting to be written to disk, and being written now |
AnonPages |
Anonymous memory mapped into processes |
Shmem |
Shared memory and tmpfs, which cannot simply be dropped |
SwapTotal, SwapFree, SwapCached |
Swap size, free swap, and pages that are in RAM and also still in swap |
Committed_AS |
Memory processes have allocated, used or not, which matters in the OOM lesson |
A large Dirty value that keeps growing means writes are arriving faster than the disk takes them, a disk problem looking like a memory one. A large Shmem value is often a tmpfs /tmp someone filled.
Swap is not a sign of failure
Swap is disk, or compressed RAM, where anonymous pages can go. A common belief is that a healthy server should have none. The argument against it: without swap, the kernel’s only way to free memory is to drop page cache. So an idle daemon’s never-touched heap pins RAM forever, while the hot data your database reads every second gets thrown out and re-read from disk. With swap, those cold pages move out and the cache stays.
What hurts is thrashing: pages constantly moving out and back because the working set does not fit. Watch the rate, not the total. vmstat 1 shows si and so, swap-in and swap-out per second. Swap used with si/so near zero is cold pages parked. Sustained high si/so is a machine that needs more RAM or less work.
swappiness
vm.swappiness is commonly explained as “the percentage of RAM in use before swapping starts”. The kernel documentation says otherwise. It is the rough relative I/O cost of swapping versus filesystem paging, a value from 0 to 200, default 60. At 100 the kernel treats both as equally expensive. Lower values mean swap is treated as more expensive, so file cache is reclaimed first. For in-memory swap such as zram, the docs say values above 100 can make sense, because swap there is cheaper than disk. At 0, the kernel does not start swapping until free and file-backed pages fall below a zone’s high watermark.
sysctl vm.swappiness # read
echo 'vm.swappiness = 10' > /etc/sysctl.d/90-swap.conf
sysctl --system # apply all sysctl.d files
The sysctl lesson covers where such settings live.
zram
zram creates a block device in RAM whose pages are compressed. Used as swap, it gives the kernel somewhere to put cold pages that is far faster than disk, at the cost of some CPU. Fedora made it the default in Fedora 33: the Fedora change page says installs get swap on /dev/zram0, set up at boot by zram-generator, and that a full device uses about half its size in RAM because of compression. It also notes this means no hibernation. On servers, zram suits small VMs and machines with no fast local disk.
drop_caches is not maintenance
echo 3 > /proc/sys/vm/drop_caches frees the page cache and reclaimable slabs. The kernel documentation calls it non-destructive, but says it “is not a means to control the growth of the various kernel caches”, that dropping them can cost significant I/O and CPU, and that use outside testing or debugging is not recommended. It is useful for one thing: making a benchmark start with a cold cache.
About the video
The embed is a clip, 3:40 to the end, of Learn Linux TV’s “Linux Crash Course - Understanding Memory and Swap Usage”, May 2022. Its chapters are the free command, understanding swap, whether you should disable swap, and adjusting swappiness. The cut skips a long intro. It is the clearest walk through free I found from an established channel. It is from 2022, so it does not cover zram, and I could not check from chapters alone how it describes swappiness, so the kernel’s definition above is the one to trust. English was judged from the channel and English auto-captions. Chris Down’s SREcon talk in deeper is the deeper case for swap, from a kernel memory-management developer.
What was run: nothing. There is no Linux machine here and macOS reports memory differently, so every field and setting above is from free(1) and proc_meminfo(5) in man-pages 6.19, the kernel 7.4-rc4 documentation for /proc/sys/vm and zram, and Fedora’s SwapOnZRAM change page, checked 2026-11-28.
Where this fits
When reclaiming cache and swapping are not enough, the kernel picks a process and kills it: the OOM killer.
Check yourself
free shows 64 GB total, 1 GB free and 50 GB available. Is the machine short of memory?
No. Most of the non-free memory is page cache that can be reclaimed. available, about 50 GB, is what new work could use without swapping.
Why can the kernel throw away a cached page of /usr/bin/python3 instantly but not a page of a Python process's heap?
The cached page is a clean copy of a file still on disk and can be read again. The heap page exists nowhere else, so it can only be moved to swap, or lost if the process dies.
A server shows 2 GB of swap used but vmstat's si and so columns are zero. Problem?
Probably not. Some idle pages were moved out once and nothing is moving now. Steady non-zero si/so under load is the sign of memory pressure.
What does vm.swappiness=10 mean?
That swap I/O is treated as much more expensive than file I/O, so the kernel prefers reclaiming page cache over swapping out anonymous memory. It does not mean 'swap when 90% of RAM is used'.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.