systemd resource control and cgroup v2: MemoryMax persistence verified, and the swap backstop (with a full retraction of an earlier misjudgment)
What you will learn
Section titled “What you will learn”The right way to fence memory/CPU for services with systemd — plus a record worth the price of admission: how this site dismantled a “scope trap” misjudgment down to the truth — the double illusion of a swap backstop plus a hog-script bug — and the one line of configuration that makes MemoryMax a real OOM hard cap.
The cgroup v2 baseline
Section titled “The cgroup v2 baseline”stat -fc %T /sys/fs/cgroup # cgroup2fs (v2 is the default now)cat /sys/fs/cgroup/cgroup.controllers# cpuset cpu io memory hugetlb pids rdma miscOL10 (systemd 257) runs cgroup v2 natively, with the memory controller available by default.
Service units: the limit persists byte-for-byte (raw output)
Section titled “Service units: the limit persists byte-for-byte (raw output)”systemd-run --unit=ol-lim -p MemoryMax=67108864 sleep 300# Running as unit: ol-lim.service; invocation ID: …
systemctl show ol-lim.service -p MemoryMax# MemoryMax=67108864 ← read back from the systemd side
CG=$(systemctl show ol-lim.service -p ControlGroup --value) # /system.slice/ol-lim.servicecat /sys/fs/cgroup/$CG/memory.max# 67108864 ← the kernel-side raw file, byte-for-byte identicalPersistent configuration (a unit file): write a [Service] section (MemoryMax/CPUQuota/TasksMax) into /etc/systemd/system/myapp.service.d/limits.conf, effective after daemon-reload.
Scope units: persisted identically (the retraction evidence)
Section titled “Scope units: persisted identically (the retraction evidence)”systemd-run --scope -p MemoryMax=67108864 python3 -c "p = open('/proc/self/cgroup').read().split('::')[-1].strip()print(open('/sys/fs/cgroup' + p + '/memory.max').read())"# scope-cgroup=/system.slice/run-p5013-i5313.scope# 67108864 ← the scope's -p reaches the kernel tooTroubleshooting note: under cgroup v2 every unit has its own cgroup subdirectory — /sys/fs/cgroup/memory.max (the root path) does not exist; to read a limit, first get your own directory from /proc/self/cgroup. This site’s original FileNotFoundError came from reading the root path and was misread as “the scope did not land on a memory-controller leaf”.
The real pit: the swap backstop
Section titled “The real pit: the swap backstop”| Scenario (MemoryMax=67108864, 200MB touched page by page) | Result | Key numbers |
|---|---|---|
| Default swap (memory.swap.max=max) | survives (hog-ok, success) | Memory peak 64M, swap peak 145.2M |
| + MemorySwapMax=0 (service) | OOM kill | result: oom-kill, status=9/KILL |
| + MemorySwapMax=0 (scope) | OOM kill (exit 137) | killed likewise, the kernel’s oom_memcg pointing at the scope cgroup |
MemoryMax is by default not an OOM hard cap: the kernel keeps swapping out over-limit pages (the swap backstop), the cgroup’s memory pinned at 64M with 145.2M in swap — the limit works but manifests as forced paging. For a hard cap:
systemd-run --unit=ol-hog3 -p MemoryMax=67108864 -p MemorySwapMax=0 python3 hog.py# Finished with result: oom-kill# kernel: Memory cgroup out of memory: Killed process 5166 (python3) … anon-rss:65280kBRetracing the misjudgment (methodology)
Section titled “Retracing the misjudgment (methodology)”This site’s first measurement recorded “a 200MB process under —scope -p did not trigger OOM” and labeled it a scope trap. Resampling revealed two stacked illusions:
- The hog-script bug:
b = bytearray(1048576); b[:] = b'x'— on a bytearray,b[:] = b'x'replaces the entire buffer with 1 byte (it does not fill it with ‘x’), shrinking the buffer so the process peaked at 4.4M and never approached the limit. The correct form isdata.append(b'\x01' * 1048576)(multiplication genuinely generates and touches 1MB). - The probe path error: reading
/sys/fs/cgroup/memory.maxinside a scope process raised FileNotFoundError — the root cgroup directory never had that file, which cannot support claiming “not on a memory leaf”.
The verification method: first read the Memory peak from systemctl status / the journal (4.4M exposes a fake hog instantly), then read the unit’s own cgroup directory. Lesson: when a conclusion wobbles, verify the measurement itself first.
The limits quick reference
Section titled “The limits quick reference”| Goal | Property | Example |
|---|---|---|
| A memory hard cap (OOM beyond it) | MemoryMax + MemorySwapMax=0 | MemoryMax=512M, MemorySwapMax=0 |
| A memory soft cap (reclaim beyond it) | MemoryHigh | MemoryHigh=400M |
| A swap cap | MemorySwapMax | MemorySwapMax=0 (no swap) |
| A CPU quota | CPUQuota | CPUQuota=50% (half a core) |
| A task count | TasksMax | TasksMax=256 |
| IO bandwidth | IOReadMax/IOWriteMax | IOReadMax=/dev/sda 50M |
Evidence and limits on this page
Section titled “Evidence and limits on this page”- Measured: the cgroup v2 controller list, MemoryMax persistence on both the service and scope paths (raw output), the swap backstop and OOM scene for a real 200MB hog (service + scope both), and the hog-script bug retrospective (E3, resampled 2026-11-10 + historical logs 2026-11-05);
- Not measured: CPUQuota/TasksMax/IO limit live scenarios (the page gives usage only), and cgroup delegation inside containers;
- Sources: systemd.resource-control(5), OL10 systemd documentation.