GrepBoard/Manuals
605 manuals ·

systemd-run MemoryHigh vs MemoryMax: throttle or OOM kill?

·Tested on Ubuntu 24.04 · systemd 255 · kernel 6.8 · cgroup v2 · 2026-10·Test scope ↓·5 steps

A 400MB allocation under systemd-run --scope with MemoryHigh or MemoryMax at 256M. With swap both finished; without swap High stalled and Max was OOM-killed.

When you wrap a heavy job in systemd-run --scope -p MemoryHigh=… or -p MemoryMax=…, the difference shows up once the limit is crossed. MemoryHigh throttles and reclaims and never calls the OOM killer. MemoryMax invokes the OOM killer inside the unit if usage cannot be reclaimed below the limit. In testing, though, whether swap is available decides the outcome more than which setting you pick.

[1] Run the same job under five settings. The job is a Python script that allocates 16MB at a time and writes every 4KB so the memory is really used, up to 400MB. case.sh runs it under timeout 90 and then prints memory.events, memory.peak and memory.swap.peak of its own cgroup. Everything ran from a root shell with --scope.

Setting (limit 256M)Result (3 runs)Timememory.eventsPeak swap
MemoryHigh3/3 finished0.5 shigh 616-618about 162MB
MemoryMax3/3 finished0.5 smax 1357-1358about 162MB
MemoryMax + MemorySwapMax=0 (default OOMPolicy)3/3 OOM, then the whole scope stopped (systemd-run exit 143)output ends after 192MB (no timing line)read from outside the scope: oom_kill 1, max 35-370 (memory.swap.max = 0)
MemoryMax + MemorySwapMax=0 + OOMPolicy=continue3/3 only the allocating process OOM-killed (exit 137)0.2 soom_kill 1, max 35-380
MemoryHigh + MemorySwapMax=03/3 stalled (90 s timeout)90 shigh 2699-42300

In the third row case.sh itself is terminated before it can print, so a watcher outside the scope read memory.events every 1ms and kept the last value before the cgroup disappeared. The fourth row was printed from inside the scope. They are separate tests.

[2] With swap, neither setting kills the job. MemoryHigh=256M:

console
$ systemd-run --scope -q -p MemoryHigh=256M -p CPUWeight=20 nice -n 10 bash case.shcgroup: run-rc64f86576af24ea59b002d5dbdb72d8b.scopememory.high = 268435456memory.max = maxmemory.swap.max = max…done 400 MB in 0.5 sexit=0  elapsed=0.6 s--- memory.eventslow 0high 616max 0oom 0oom_kill 0oom_group_kill 0memory.peak = 268914688memory.swap.peak = 161742848

MemoryMax=256M finished in the same time with oom_kill 0 and the same 162MB of swap. In this test the excess moved to swap and never reached an OOM kill. What MemoryMax itself means is "if usage cannot be reclaimed below the limit, invoke the OOM killer inside the unit" (memory.max in the kernel cgroup v2 docs). Even with free swap, a lot of unreclaimable memory or a lower limit on a parent slice can still end in OOM, so do not count on MemoryMax alone to "finish thanks to swap". The 0.5 seconds is just what this server showed; swap device speed was not measured.

[3] Without swap, MemoryMax kills right away. By default the shell started by systemd-run was terminated too, so case.sh never printed its results (nothing after the 192MB line). This is the run with a watcher reading memory.events from outside the scope every 1ms:

console
$ systemd-run --scope -q --unit=gblab-n29w-3-max-noswap-1 -p MemoryMax=256M -p MemorySwapMax=0 -p CPUWeight=20 nice -n 10 bash case.shcgroup: gblab-n29w-3-max-noswap-1.scopememory.high = maxmemory.max = 268435456memory.swap.max = 0  64 MB     0.0 s 128 MB     0.1 s 192 MB     0.1 s(systemd-run exit=143)--- memory.events (last value read from outside the scope, every 1ms)low 0high 0max 35oom 1oom_kill 1oom_group_kill 0

oom_kill 1 shows the allocating process was OOM-killed, and systemd-run's 143 (SIGTERM) shows the rest of the scope was then stopped. A scope's OOMPolicy= defaults to DefaultOOMPolicy= (normally stop). To find an OOM kill that already happened on your server, use journalctl -k | grep 'Memory cgroup out of memory'.

If you need to read results inside the same scope, pass OOMPolicy=continue. This command deliberately triggers an OOM kill. Do not apply it to production services or jobs that are writing data; use it only on a throwaway test process. Name the scope (--unit=memtest) so that, if it hangs, sudo systemctl stop memtest.scope from another terminal stops just that scope, and afterwards confirm systemctl status memtest.scope reports "could not be found" (if it is left in failed state, clear it with sudo systemctl reset-failed memtest.scope).

console
$ systemd-run --scope -q -p MemoryMax=256M -p MemorySwapMax=0 -p OOMPolicy=continue -p CPUWeight=20 nice -n 10 bash case.sh…case.sh: line 8: 563079 Killed                  timeout 90 python3 alloc.py 400exit=137  elapsed=0.2 s--- memory.eventslow 0high 0max 35oom 1oom_kill 1oom_group_kill 0memory.peak = 268435456memory.swap.peak = 0

[4] Without swap, MemoryHigh alone neither kills nor finishes. Progress stopped around 256MB, and timeout 90 ended all three runs.

console
$ systemd-run --scope -q -p MemoryHigh=256M -p MemorySwapMax=0 -p CPUWeight=20 nice -n 10 bash case.sh… 256 MB     0.2 sexit=124  elapsed=90.7 s--- memory.eventslow 0high 4230max 0oom 0oom_kill 0oom_group_kill 0memory.peak = 296009728memory.swap.peak = 0

memory.peak above the limit (268435456) matches the documentation's "memory usage may go above the limit if unavoidable". Anonymous memory with nowhere to go can only be throttled, so only the high counter grew. Without a timeout or a watcher, a job under this setting just hangs.

[5] Pick by goal.

GoalSetting
Finish even if slow (with spare swap)MemoryHigh= plus a generous MemorySwapMax=
Stop it when exceeded (only jobs that may be killed)MemoryMax= plus MemorySwapMax=0
Clean up in the same scope after a killAdd OOMPolicy=continue
BothMemoryHigh below MemoryMax (the combination the systemd docs recommend)

While a job runs, watch it from another terminal with systemctl status run-….scope and /sys/fs/cgroup/system.slice/run-….scope/memory.events. For an OOM kill that already happened, search the journal as in find what the OOM killer killed.

#systemd-run#cgroup#memory#oom#swap