Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
systemctl set-property shop-api MemoryHigh=800M MemoryMax=1G CPUQuota=150% TasksMax=512
systemctl show shop-api -p MemoryMax -p CPUQuotaPerSecUSec -p TasksMax
systemd-cgtop -m
systemd-run --scope -p MemoryMax=500M -p CPUQuota=50% tar -czf /backup/big.tar.gz /srv/dataset-property saves the limits as a drop-in and applies them at once (no restart).
Step by step
- The useful limits:
| Setting | Means |
|---|---|
MemoryMax=1G | Hard limit — above it the kernel kills a process of this service (OOM). |
MemoryHigh=800M | Soft limit — the service is slowed and memory is reclaimed first. |
CPUQuota=150% | At most 1.5 CPU cores. |
CPUWeight=50 | Lower priority when CPU is busy (default 100). |
TasksMax=512 | Max processes/threads (stops fork bombs). |
IOWeight=50 | Lower disk priority (backups, batch jobs). |
- Set them in a drop-in (with
set-propertyor by hand):
[Service]
MemoryHigh=800M
MemoryMax=1G
CPUQuota=150%
TasksMax=512systemctl daemon-reload- Watch usage live — per service:
systemd-cgtop -m
Control Group Tasks %CPU Memory
/system.slice/shop-api.service 9 38.2 712.4M
systemctl status shop-api --no-pager | grep -E 'Memory|CPU|Tasks'- What happens at the limit: at
MemoryHighthe service gets slower (reclaim); atMemoryMaxthe kernel OOM-kills inside the service only — the rest of the server is safe. WithRestart=on-failureit comes back:
journalctl -u shop-api | grep -iE 'oom|memory'- Limit a one-off command (a big backup, a report) —
systemd-runputs it in its own cgroup:
systemd-run --scope -p MemoryMax=500M -p CPUQuota=50% -p IOWeight=20 tar -czf /backup/big.tar.gz /srv/data- Set limits from measured usage — look at normal and peak use for a few days (Prometheus or
systemd-cgtop), then putMemoryMaxabove the normal peak.
Check that it worked
systemctl show shop-api -p MemoryMax shows 1073741824, systemd-cgtop shows the service under its limits, and a test with heavy load stays inside the limits without slowing other services.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| The service gets OOM-killed often | MemoryMax too low (or a leak). | Measure real use; raise the limit or fix the leak; set MemoryHigh below MemoryMax. |
| Limits have no effect | cgroup v1 system, or another unit/slice overrides it. | Check stat -fc %T /sys/fs/cgroup (cgroup2fs = v2); systemctl cat. |
CPUQuota makes the app slow at peaks | Quota too tight. | Raise it, or use CPUWeight (only matters when CPU is busy). |
Fork errors (Resource temporarily unavailable) | TasksMax too low for the app's threads. | Raise TasksMax. |
| Java/Node ignores the limit and gets killed | The runtime sizes memory by host RAM. | Set the runtime's own limit (-Xmx, --max-old-space-size) below MemoryMax. |
set-property change lost | Used --runtime. | Without --runtime it is saved permanently. |
Go further
- Command pages: systemctl, systemd-run, systemd-cgls.
- Next how-tos: Create a systemd service for your app, Check memory and CPU usage, Run a container with memory and CPU limits.
- Learn it: RHCSA — Slices and cgroups, RH134 — Priorities with nice.
1What is the difference between MemoryHigh and MemoryMax?
MemoryHigh slows the service and reclaims memory; MemoryMax is the hard limit where the kernel kills a process.