Virtual memory, page faults and the OOM killer
Every process sees its own clean address space. Behind it, the kernel maps pages lazily, shares them when it can, and kills someone when it runs out.
Addresses in a process are virtual. The kernel divides memory into pages, usually 4 KiB, and keeps a page table per process mapping virtual pages to physical frames. Two processes can use the same virtual address for different data, and the same physical frame can appear in several processes, such as a shared library.
Two processes, one pool of physical memory
fork, parent and child share pages marked copy-on-write until one of them writes. Pages never touched have no frame at all.Page faults
Memory is allocated lazily. malloc of 1 GB reserves address space; frames arrive only on first touch. That first touch causes a minor page fault, which the kernel handles in microseconds. A major fault needs disk, for a page of a memory-mapped file that is not in RAM or a page in swap, and costs far more.
mmap and the page cache
The kernel keeps recently used file data in the page cache. read copies from the page cache into your buffer. mmap maps the file's cached pages straight into your address space, so reading memory reads the file. The second time you read a file, it usually comes from RAM, which is why benchmarks must say whether the cache was warm.
Copy-on-write
fork does not copy memory. Parent and child share every page, marked read-only. The first write to a page faults, and the kernel copies just that page. This makes fork cheap for small processes. For a parent with a large heap it still costs page-table copying, and heavy writes after forking can double memory, as Redis snapshots famously show.
The OOM killer
Linux overcommits: it grants more virtual memory than it has. When physical memory and swap run out, the OOM killer picks a victim by a score based mainly on memory use and sends SIGKILL. Inside a container, the cgroup memory limit triggers the same thing for that cgroup only. The victim gets no warning and writes no error message, only exit status 137.
# Resident vs virtual size of a process
ps -o pid,rss,vsz,cmd -p $PID
# Minor and major page faults so far
ps -o min_flt,maj_flt -p $PID
# Recent OOM kills
dmesg | grep -i 'killed process'
# Inside a container (cgroup v2)
cat /sys/fs/cgroup/memory.max /sys/fs/cgroup/memory.eventsmemory.events counts oom_kill events for the cgroup, which confirms a silent 137.
Where you will meet it
Postgres relies on the OS page cache in addition to its own buffers, which is why it is configured to use only part of RAM directly.
MicroVM platforms restore snapshots with copy-on-write memory, so many sandboxes can start from one image in milliseconds (Module 12).
Node's --max-old-space-size limits the V8 heap. Set it below the container limit, or the kernel kills the process before V8 can report an error.
Check yourself
Answer out loud before you open each one.
Why does malloc(1 GB) succeed instantly on a machine with little free memory?
It only reserves virtual address space. Physical frames are allocated on first touch.
What is copy-on-write?
After fork, pages are shared read-only, and a page is copied only when one process writes to it.
A container process vanished with status 137 and no log. Where do you look?
memory.events for the cgroup and the kernel log for an OOM kill.
Try it
Allocate a 500 MB Buffer.allocUnsafe in Node and check RSS with ps. Then fill it with buf.fill(1) and check again.