Linux Labs

Inside the system: resources, kernel, processes / Stage 2 of 12

Memory, swap and the OOM killer

Not running Starting usually ~20 s
Leave the lesson

Step 3 of 9 · on a machine · 4 min

Swap

The Swap line in free on the lesson machine is all zeros. What this memory is and what the machine lacks without it.

The kernel can simply drop the file cache: the file is on the disk and can be read again. The data of the processes themselves — variables, loaded pictures, buffers — cannot be fetched again from anywhere. It needs either RAM or swap — an area on the disk (a partition or a file) where the kernel moves rarely used pages of processes.

Swap gives a reserve, but a slow one: a page in swap is read from the disk. A machine that keeps moving pages to swap and back slows down.

Without swap, process data can live only in RAM. When available on the whole machine approaches zero and there is nothing more to drop, the kernel calls the OOM killer: it picks a process and ends it to free memory. Two commands show whether there is swap:

bash
swapon --show
free -h

swapon --show prints nothing — there is no swap. You'll add a swap file in this module's lab.

Check yourself 1 question

On a machine without swap, a process asks for 200 MiB more while available is 150 MiB. What happens?

  • The kernel does not wait for other processes to give memory back on their own. Once the cache is dropped and memory is still short, the OOM killer comes.

  • The kernel can move process data to the disk only into swap, and there is none. It drops the file cache, but the process data has nowhere to go.

Without swap, process data lives only in RAM. The kernel drops the cache first, then the OOM killer ends a process to free memory.

Key points

  • The kernel drops the file cache on demand, while process data has nowhere to live without swap except RAM.
  • Swap is a disk area for rarely used process pages: a memory reserve, but a slow one.
  • Memory runs out and there is nothing to drop — the OOM killer ends a process.
Ubuntu 24.04
Loading…