What is virtual memory?
Every process sees its own private address space, as if it had the machine to itself. The kernel maps pieces of that space, called pages and usually 4 KiB each, to physical RAM only when the process touches them. This is why a program can reserve gigabytes at start-up while using little real memory, and why top shows two numbers: VIRT, what the process has mapped, and RES, what actually sits in RAM. Only RES costs you memory.
What is overcommit and how is it set?
Because programs often reserve more than they use, Linux allows the total of reservations to exceed physical memory. The setting vm.overcommit_memory controls how generous it is:
| Value | Behavior | When to use |
|---|---|---|
0 (default) | Heuristic: refuse only obviously impossible requests | Most servers |
1 | Always allow | Redis and Valkey, which fork for snapshots |
2 | Strict: limit to swap plus a ratio of RAM | Systems that must never run the OOM killer |
Change it with sysctl -w vm.overcommit_memory=1 and persist it in a file under /etc/sysctl.d/. Mode 2 sounds safest but often makes large programs fail at start-up; test before using it.
Why is so much memory used by cache?
Because unused RAM is wasted RAM. The kernel keeps recently read files in the page cache, so the next read comes from memory. When programs need memory, clean cache pages are dropped instantly. This is why free shows low free memory on a healthy server; the available figure is the one to watch, as Check memory usage explains. Dropping caches by hand with echo 3 > /proc/sys/vm/drop_caches only makes the next reads slower; it fixes nothing.
What does swappiness change?
vm.swappiness, from 0 to 200 on current kernels with a default of 60, sets how willing the kernel is to swap out idle program memory instead of dropping file cache. Lower values keep program memory in RAM longer; a value around 10 suits database servers whose own caches matter more than the file cache. It does not disable swap. Swap setup is covered in Swap on Linux.
What are huge pages?
Larger pages, 2 MiB or 1 GiB instead of 4 KiB, which cut the overhead of address translation for programs with big memory footprints. There are two kinds:
- Explicit huge pages, reserved with
vm.nr_hugepagesand used by programs that ask for them. PostgreSQL'shuge_pages = tryand database buffer pools benefit, as do virtual machines. - Transparent huge pages (THP), applied automatically by the kernel. Helpful for some workloads, harmful for others: Redis, Valkey and some databases recommend
madviseornever.
cat /sys/kernel/mm/transparent_hugepage/enabled echo madvise > /sys/kernel/mm/transparent_hugepage/enabled
The second command lasts until reboot; persist it with a kernel parameter or a small systemd unit.
How do I cap a single service's memory?
With cgroups, through systemd. A drop-in for the unit sets soft and hard limits:
systemctl edit reportgen.service # [Service] # MemoryHigh=6G # MemoryMax=8G
Above MemoryHigh, the service is slowed and its memory reclaimed aggressively; at MemoryMax, the kernel's OOM killer acts inside that service only. One runaway report generator then cannot push the database out of memory. Check usage with systemctl status reportgen or systemd-cgtop.
What happens when memory truly runs out?
The OOM killer picks a process, usually the largest one with the highest score, and kills it so the system survives. Each process has an oom_score_adj from -1000 to 1000; services can protect themselves with OOMScoreAdjust=-500 in their unit, or volunteer with a positive value. Many systems also run systemd-oomd, which acts earlier based on memory pressure. All kills are logged in the kernel journal.
How does NUMA fit in?
On dual-socket machines, each processor has local memory and reaches the other socket's memory more slowly. The kernel prefers local allocations; a process that outgrows its node either uses remote memory or triggers reclaim locally, depending on settings. For large databases on Dual CPU line or High memory line machines, check balance with numastat and read NUMA and dual-socket servers. On single-socket models such as S1, there is one node and nothing to tune.
Keep Reading
Check memory usage
Read free, /proc/meminfo, vmstat and per-process numbers correctly, and tell real memory pressure apart from a healthy page cache.
OpenSwap on Linux
What swap is for on a RAM-rich server, how to add a swap file on ext4 or XFS, zram as an alternative and how to tell useful swap from thrashing.
OpenThe memory hierarchy
From registers through L1, L2 and L3 caches to RAM, NVMe and the network: how each level trades speed for size, and why it shapes performance.
OpenNUMA and dual-socket servers
Why two processors in one box share memory unevenly, and when a dual-socket machine beats a single big chip.
OpenFAQ
Quick Answers
VIRT is address space a process has mapped; RES is memory actually in RAM. Only RES costs real memory.
No. Cache is released automatically when programs need memory; dropping it only slows later reads.
Around 10 is a common choice, keeping program memory in RAM while still allowing swap as a safety net.
Set MemoryHigh and MemoryMax in a systemd drop-in for that unit.
For Redis, Valkey and some databases, set it to madvise or never as their documentation recommends.