CSE Roadmap

Core tier · about 5 hours · 7 parts

Part 3 of 7 · 3 min read

Overcommit

In plain words: An airline sells more seats than the plane has, since some passengers never turn up. The kernel does the same with memory. The call mmap (or a malloc that uses it) only reserves address space. Frames are given at the first touch (Level 6 page 7). A program that asks for much and uses little costs the machine little, so the kernel accepts requests that add up to more than the machine's memory. This is overcommit. The analogy stops in one place: an airline can send passengers away at the gate, and the kernel finds the shortage only after the program has been running, when it touches a page and no frame is free.

Two requests on the test machine, which has 8 GiB of memory and no swap:

Request Result
mmap of 6 GiB, anonymous, private succeeded, VmSize 6,294,164 kB, VmRSS 1,792 kB
mmap of 100 GiB failed with "Cannot allocate memory"

The first call was accepted, and used almost no memory until pages were touched. The second was refused because it was far too large for the machine. This is the default mode, heuristic overcommit (vm.overcommit_memory is 0): the kernel refuses a single request that is obviously more than all memory and swap, and accepts the rest. The setting has three values:

Value Meaning
0 (default) heuristic: refuse obvious overcommits
1 always accept, whatever the size
2 strict: the total promised may not pass a limit, CommitLimit

In mode 2, the limit is the swap space plus a share of the memory, set by vm.overcommit_ratio (50 on the test machine). With 8 GiB and no swap, that is about 4 GiB (a simplified calculation). The file /proc/meminfo shows CommitLimit and Committed_AS, the amount promised now.

Why allow it? Thread stacks show the reason. A program with 100 threads, each with the default 8 MiB stack (page 3), reserves about 800 MiB. The test showed VmSize growing from 2,708 kB to 822,308 kB, and VmRSS growing only from 1,480 kB to 2,692 kB. The reserved space was nearly all unused. A strict kernel would refuse such programs on a small machine. The same holds for fork of a big process, and for sparse arrays.

Overcommit

Overcommit is the kernel's practice of accepting requests for more memory, in total, than the machine can supply, on the expectation that programs will touch only part of it, and of finding any real shortage only when a page is touched.

Example: a 6 GiB mapping was accepted on an 8 GiB machine and cost 1,792 kB until its pages were used.

Common mistake

"malloc returned a pointer, so the memory is there" is false on Linux with default settings. The failure may come later, at the first touch of a page, as a killed process and not as a NULL return.

Check yourself: 1. Why did the 6 GiB request succeed and the 100 GiB request fail? 2. What does mode 2 of vm.overcommit_memory change? 3. Why is it useful to allow overcommit?