Sub topic 5
Controlling swap with --memory-swap
This chapter assumes you can start containers with docker run, that you know --memory sets a hard memory limit, and that you know what swap is. Swap is disk space the kernel uses as overflow when RAM is full.
What the flag actually means
--memory-swap is the total of memory plus swap. It is not the amount of swap alone. This is the most common mistake with the flag.
docker run -d --name buffered --memory 100m --memory-swap 300m alpine sleep 3600The container may use 100 MiB of RAM and 200 MiB of swap, which is 300 MiB in total.
How the values combine
The result depends on how you set the two flags.
| Flags | Effect |
|---|---|
| `--memory 100m` only | Swap is also 100 MiB, so the total is 200 MiB. |
| `--memory 100m --memory-swap 300m` | Swap is 200 MiB, the difference between the two. |
| `--memory 100m --memory-swap 100m` | No swap. The total equals the RAM limit. |
| `--memory 100m --memory-swap -1` | Swap is unlimited, up to what the host has. |
If you set a positive --memory-swap, you must also set --memory. The default of "RAM plus the same amount of swap" applies only when the host has swap enabled. Without host swap, the container can use RAM only.
Seeing the effect
Setting both flags to the same value turns swap off. When the process passes the limit, the kernel has nowhere to push pages and kills it.
docker run --name hog --memory 50m --memory-swap 50m alpine sh -c 'tail /dev/zero'
docker inspect --format '{{.State.OOMKilled}} {{.State.ExitCode}}' hogtail /dev/zero buffers input in memory without end. The inspect output is typically true 137.
With a larger --memory-swap, the same program survives longer. It spills into swap first, runs much slower, and is killed only after the total is used up.
Why you might disable swap
Swap keeps a container alive, but it can hide a problem. A database that swaps heavily becomes slow instead of failing. Slowness is harder to notice than a restart. For latency-sensitive services, many teams set --memory-swap equal to --memory. The container then fails fast, and a restart policy or orchestrator can replace it.
Swap is more useful for batch jobs with occasional memory spikes, where finishing slowly beats being killed.
Tuning how eagerly swap is used
--memory-swappiness takes a value from 0 to 100. It tells the kernel how willing it is to move a container's pages to swap. A value of 0 turns swapping of anonymous memory off as far as the kernel allows. It does not guarantee a limit, so use --memory-swap when you need a hard rule.
Common mistakes
- Reading
--memory-swap 300mas "300 MiB of swap". It means 300 MiB in total. - Setting
--memory-swapbelow--memory. Docker rejects this. - Changing only one of the two flags in
docker update. Set both together so the values stay consistent. - Testing on a host with no swap, then expecting the same behavior in production.
Exercise
A container runs with --memory 256m --memory-swap 512m. How much swap can it use, and what happens if you change --memory-swap to 256m?
Show answer
It can use 256 MiB of swap, which is 512 MiB total minus 256 MiB of RAM. With --memory-swap 256m the total equals the RAM limit, so swap is disabled. Once the container exceeds 256 MiB, the kernel kills it with an out-of-memory kill, usually exit code 137.