December 5, 2026

Limit a Process’s Memory and CPU on Linux: systemd-run, cgroups, and What Each Limit Does When It Is Hit

Glass beakers of blue liquid with one marked by a red limit line at the rim

A Python script asked for 200 MB, one megabyte at a time. Inside a control group capped at 50 MB, it printed “allocated 40 MB” and then stopped mid-sentence, killed with signal 9. Another script, spinning in a loop for three seconds, ran at exactly half speed under a 50% CPU cap. A shell trying to start 40 background jobs under a 20-process cap got “Cannot fork”. Those are the three limits most worth knowing, and they behave in three different ways. systemd-run is the convenient way to apply them to any command; the kernel feature underneath, control groups, is what was tested here.

Video: linuxfestnorthwest on YouTube

What does the limiting

Control groups

Linux enforces resource limits per group of processes, not per process. A control group, or cgroup, holds a set of processes, and limits set on the group apply to all of them together, including any children they start. systemd puts every service, and every login session, into its own cgroup, which is how it can stop a service completely or report its memory use.

How the tests were run

The test machine for this post ran without systemd as its init and exposed the older version 1 cgroup interface, so the limits were applied by writing to the cgroup files directly rather than through systemd-run. The kernel mechanism is the same one that systemd-run drives on a modern system using version 2; the setting names differ (version 1’s memory.limit_in_bytes is version 2’s memory.max), and the systemd property names below map onto them. The behavior on hitting each limit is what the tests show.

Dripping faucet

systemd-run

A transient unit for one command

The systemd-run manual describes the tool as a way to create and start a transient .service or .scope unit and run a command in it. Transient means the unit exists only while the command runs; nothing is written to /etc/systemd. Limits are passed with -p (--property), which sets a property on the unit that is created.

Two forms matter:

# As a background service unit (the default)
systemd-run -p MemoryMax=500M -p CPUQuota=50% --unit=nightly-job /usr/local/bin/job.sh

# As a scope, attached to the current terminal
systemd-run --scope -p MemoryMax=500M -p CPUQuota=50% ./build.sh

The --scope option creates a transient scope unit instead of a service, so the command runs in the foreground of the current shell, with output on the terminal, but inside its own cgroup with its own limits. For an interactive one-off, --scope is usually what is wanted. Adding --user talks to the user’s own service manager instead of the system’s, which on many systems lets an unprivileged user limit their own commands.

Memory: the hard cap kills

MemoryMax

The systemd resource-control manual describes MemoryMax= as the absolute limit on memory usage: if usage cannot be contained under it, the out-of-memory killer is invoked inside the unit. That is what the test saw. With the group capped at 50 MB, the allocating script reported progress at 0, 20 and 40 MB, then died. Its exit status was −9, SIGKILL. The group’s counters afterward:

max usage 52428800    (exactly 50 MiB)
failcnt   39          (allocations that hit the limit)
oom_kill  1

Nothing outside the group was touched. That containment is the point: a runaway job takes itself down instead of pushing the whole machine into swap or provoking the kernel to kill something important.

MemoryHigh: slow down instead

The same manual recommends a softer setting as the main control. MemoryHigh= is a throttling limit: usage may go above the limit if unavoidable, but the processes are heavily slowed down and memory is taken away aggressively. It calls this the main mechanism to control a unit’s memory, and advises using MemoryMax= as the last line of defense. A common pairing sets MemoryHigh a little below MemoryMax, so a job under pressure slows and gives back memory before it is killed.

Data center server racks

CPU: the cap throttles

CPUQuota

CPUQuota= takes a percentage. The manual explains that it is relative to the total CPU time available on one CPU, so 50% is half of one core and 200% is two full cores. On version 2 it controls the cpu.max attribute.

The test ran the same three-second busy loop twice:

Run Loop iterations CPU time used Wall time
No limit 26,574,417 2.98 s 3.02 s
50% quota 13,872,339 1.53 s 3.04 s

Same wall-clock time, half the CPU, about half the work. The kernel enforced it by letting the group run for its 50 ms share of each 100 ms period and then pausing it; the group’s statistics recorded 30 throttled periods. Unlike the memory cap, nothing failed. The job simply ran slower.

When CPU limits are the right tool

That behavior makes CPUQuota safe to apply to almost anything: backups, compression, indexing, a build on a shared machine. The job finishes later, and everything else on the machine stays responsive. For a job that must finish by a deadline, the cost is time, and it is predictable: a job that takes 10 minutes of one CPU takes 20 at 50%.

Process count: the cap refuses

TasksMax

The third limit counts processes and threads. The manual describes TasksMax= as the maximum number of tasks that may be created in the unit, as an absolute number or a percentage of the system maximum.

The test capped a group at 20 and had a shell inside it try to start 40 background sleep processes. The shell got as far as the limit allowed and then reported:

sh: 0: Cannot fork

The group’s event counter recorded the limit being hit. Existing processes kept running; new ones were refused. That is the containment a fork bomb or a runaway worker pool needs: the damage stays inside the group, and the rest of the machine can still start processes, including the shell an administrator needs to clean up.

Subway station with multiple turnstiles

Making limits permanent for a service

In the unit file

For a service that should always run with limits, the same properties go in a drop-in rather than on a systemd-run command line:

# /etc/systemd/system/myapp.service.d/limits.conf
[Service]
MemoryHigh=400M
MemoryMax=500M
CPUQuota=150%
TasksMax=200

After systemctl daemon-reload and a restart, systemctl status myapp shows current memory use against the limit. Pairing MemoryMax with automatic restarts, covered in the earlier post on Restart= and RestartSec=, gives a service that is killed when it leaks and comes back on its own.

Not the same as ulimit

These limits are different from the per-process limits set with ulimit, described in the earlier post on too many open files. A ulimit applies to each process separately, and each child gets its own copy. A cgroup limit applies to the whole group at once: ten processes under MemoryMax=500M share 500 MB between them, and no amount of forking escapes it.

A second test made that concrete. Two copies of a script that each needed 30 MB were started in one group capped at 50 MB. Together they needed 60. Neither finished: both exited with status −9, and the group recorded two out-of-memory kills. Run again, each in its own 50 MB group, both printed “holds 30 MB”, slept, and finished normally. The limit had nothing to do with either process on its own; it was the total that broke it.

That cuts both ways. A shared cap is the right tool for a service whose workers must, together, stay inside a budget. It is the wrong tool for two unrelated jobs that happen to be launched together, because one job’s spike becomes the other’s kill.

Checking what a limit is doing

Every limit leaves evidence. In the tests, the memory group kept a high-water mark (max usage), a count of allocations that hit the ceiling (failcnt) and a count of OOM kills; the CPU group counted throttled periods; the process-count group recorded each refused fork as an event. On a system using version 2 cgroups, the equivalents sit in files such as memory.events and cpu.stat inside the unit’s cgroup directory, and systemd-cgtop shows live usage per unit. Reading those counters after a job misbehaves turns a guess about why it died into a fact. A job that exited with status 137, which is 128 plus signal 9, and an oom_kill counter that just went up by one, did not crash on its own. It was killed by a limit, and the fix is either a larger limit or a smaller job. A job that merely ran slowly with a climbing throttled count is not broken at all; it is doing exactly what its CPU quota allows.

Which limit for which problem

Choosing

  • A job that might use too much memory: MemoryHigh to slow it, MemoryMax as the hard stop. Expect a SIGKILL when the hard stop is reached.
  • A job that hogs the CPU: CPUQuota. Expect it to take longer, not to fail.
  • A job that might spawn without bound: TasksMax. Expect its next fork to fail.
  • A one-off command: systemd-run --scope -p ... in front of it.
  • A service: the same properties in a drop-in file.

Sources

  • systemd-run, systemd documentation (freedesktop.org): https://www.freedesktop.org/software/systemd/man/latest/systemd-run.html
  • systemd.resource-control, systemd documentation (freedesktop.org): https://www.freedesktop.org/software/systemd/man/latest/systemd.resource-control.html

719 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *