Skip to content
Search Oracle Linux

    Browse this documentation

    systemd resource control and cgroup v2: MemoryMax persistence verified, and the swap backstop (with a full retraction of an earlier misjudgment)

    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.

    Terminal window
    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 misc

    OL10 (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)”
    Terminal window
    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.service
    cat /sys/fs/cgroup/$CG/memory.max
    # 67108864 ← the kernel-side raw file, byte-for-byte identical

    Persistent 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)”
    Terminal window
    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 too

    Troubleshooting 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”.

    Scenario (MemoryMax=67108864, 200MB touched page by page)ResultKey numbers
    Default swap (memory.swap.max=max)survives (hog-ok, success)Memory peak 64M, swap peak 145.2M
    + MemorySwapMax=0 (service)OOM killresult: 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:

    Terminal window
    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:65280kB

    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:

    1. 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 is data.append(b'\x01' * 1048576) (multiplication genuinely generates and touches 1MB).
    2. The probe path error: reading /sys/fs/cgroup/memory.max inside 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.

    GoalPropertyExample
    A memory hard cap (OOM beyond it)MemoryMax + MemorySwapMax=0MemoryMax=512M, MemorySwapMax=0
    A memory soft cap (reclaim beyond it)MemoryHighMemoryHigh=400M
    A swap capMemorySwapMaxMemorySwapMax=0 (no swap)
    A CPU quotaCPUQuotaCPUQuota=50% (half a core)
    A task countTasksMaxTasksMax=256
    IO bandwidthIOReadMax/IOWriteMaxIOReadMax=/dev/sda 50M
    • 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.