Workspace

Virtual memory, page faults and the OOM killer

3 day streakD

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.

DepthLesson 6 of 716 min

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.

Figure 6.1

Two processes, one pool of physical memory

Virtual pages and physical framesProcess A and process B each map five virtual pages. Both map libc code to one shared frame and a heap page to one copy-on-write frame. The heap page B wrote to was copied to its own frame. A maps a file page from the page cache. Untouched pages have no frame.Process A · parentPhysical framesProcess B · after forkF0libc codeF1freeF2heap pageF3A: heapF4B: heap copyF5B: stackF6file pageF7freelibc codeheap · COWheap · writtenmmap'd fileuntouchedno frame: first touch faultslibc codeheap · COWheap · copiedstackuntouchedno frame: first touch faultssharedcopy-on-writeprivate
Each process has its own page table. Shared library code maps to one frame. After 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.

inspect-memory.shShell
# 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.events

memory.events counts oom_kill events for the cgroup, which confirms a silent 137.

Where you will meet it

Database caches

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.

Sandbox snapshots

MicroVM platforms restore snapshots with copy-on-write memory, so many sandboxes can start from one image in milliseconds (Module 12).

Language runtimes

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.

A good place to start

Explore a pattern

A little guidance

Start with a pattern, read through a problem, and mark it solved when the idea clicks. Save anything you want to revisit.

New tutorials are added one at a time. Your bookmarks and solved checkmarks stay in this browser.

Explore the roadmap