Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 106System · systemd

How to limit CPU and memory of a service

One service with a memory leak or a runaway job can slow down the whole server — or make the kernel kill the database. systemd uses cgroups to give each service limits: a memory ceiling, a CPU share, a maximum number of processes. This how-to sets limits on a service and on a one-off command, watches usage live, and explains what happens when a limit is hit.

  • 5 Lessen
  • 6 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

106.1

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/data

set-property saves the limits as a drop-in and applies them at once (no restart).

106.2

Step by step

  1. The useful limits:
SettingMeans
MemoryMax=1GHard limit — above it the kernel kills a process of this service (OOM).
MemoryHigh=800MSoft limit — the service is slowed and memory is reclaimed first.
CPUQuota=150%At most 1.5 CPU cores.
CPUWeight=50Lower priority when CPU is busy (default 100).
TasksMax=512Max processes/threads (stops fork bombs).
IOWeight=50Lower disk priority (backups, batch jobs).
  1. Set them in a drop-in (with set-property or by hand):
/etc/systemd/system/shop-api.service.d/limits.conf
[Service]
MemoryHigh=800M
MemoryMax=1G
CPUQuota=150%
TasksMax=512
systemctl daemon-reload
  1. 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'
  1. What happens at the limit: at MemoryHigh the service gets slower (reclaim); at MemoryMax the kernel OOM-kills inside the service only — the rest of the server is safe. With Restart=on-failure it comes back:
journalctl -u shop-api | grep -iE 'oom|memory'
  1. Limit a one-off command (a big backup, a report) — systemd-run puts 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
  1. Set limits from measured usage — look at normal and peak use for a few days (Prometheus or systemd-cgtop), then put MemoryMax above the normal peak.

106.3

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.

106.4

Common problems

ProblemCauseFix
The service gets OOM-killed oftenMemoryMax too low (or a leak).Measure real use; raise the limit or fix the leak; set MemoryHigh below MemoryMax.
Limits have no effectcgroup 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 peaksQuota 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 killedThe runtime sizes memory by host RAM.Set the runtime's own limit (-Xmx, --max-old-space-size) below MemoryMax.
set-property change lostUsed --runtime.Without --runtime it is saved permanently.

106.5

Go further

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.

2How do you run one big command with a memory limit?

Alle commando's in dit hoofdstuk