Srijan Guchhait

Keeping a laptop usable with memory caps for background jobs

How to cap the memory, CPU and disk priority of a background job on Linux with systemd-run, so a runaway process gets killed instead of freezing your machine.

  • linux
  • systemd
  • reliability

A background job that eats all your memory makes the whole machine unusable. The window manager stalls, the shell stops answering, and the kernel may kill the wrong process. The better outcome is that only the job dies. Linux can do that for you with a memory cap on the job.

I tested every command below on a Linux server with systemd and cgroup v2. They work the same on a laptop with the same setup. I did not test other setups.

The cap

systemd-run starts a command in its own scope and applies limits to it. No root access is needed for a user scope.

systemd-run --user --scope -q -p MemoryMax=100M -p MemorySwapMax=0 ./job

MemoryMax is a hard limit. When the job goes over it, the kernel kills the job. MemorySwapMax=0 stops the job from escaping into swap, which would turn a fast failure into a slow, noisy machine.

I tried it with a Python one-liner that asks for 400 MB.

systemd-run --user --scope -q -p MemoryMax=100M -p MemorySwapMax=0 \
  python3 -c 'x=bytearray(400*1024*1024); print("allocated 400 MB")'

It printed nothing and the process died with signal 9. The same line with 20 MB printed allocated 20 MB and exited cleanly. My shell stayed responsive the whole time.

One warning about testing this. If you pipe the output into tail, the exit status of the job is hidden, and a killed job looks like a quiet success. Check the status directly.

Check that the cap applied

Ask the cgroup what it holds. This runs a shell in a scope and reads the limit file.

systemd-run --user --scope -q -p MemoryMax=100M \
  bash -c 'cat /sys/fs/cgroup$(cut -d: -f3 /proc/self/cgroup)/memory.max'

It printed 104857600, which is 100 MiB in bytes.

A softer limit first

MemoryHigh is a throttle. When the job passes it, the kernel slows the job down and reclaims its memory, and nothing is killed. I set it below MemoryMax, so the job feels pressure before it hits the wall.

systemd-run --user --scope -q -p MemoryHigh=80M -p MemoryMax=100M ./job

Make it polite

Memory is only one resource. A background job can also starve the foreground for CPU and disk. These properties lower its share.

systemd-run --user --scope -q -p CPUWeight=20 -p IOWeight=20 nice -n 10 ./job

I ran this form with MemoryHigh=80M added, and it accepted every property and ran the command. The default weight is 100, so 20 means the job gets a smaller share when others compete. If nothing else wants the CPU, the job still runs at full speed.

The older tool: ulimit

ulimit -v limits the address space of a process, and it needs no systemd.

(ulimit -v 200000; python3 -c 'x=bytearray(400*1024*1024)')

The same 400 MB allocation raised MemoryError in Python. I use it as a fallback, but it has a catch. It limits virtual memory, not what the process actually uses. Programs that map a lot of address space they never touch can fail under a limit that is far too low. A cgroup cap counts real memory, so it fits the problem better.

Reading a failure

A job that dies under its cap does not say why. It just stops, and the error you see may come from a script that was waiting on it. When a job vanishes without a message, I look at its exit status before anything else. A status that points to signal 9 means the kernel killed it, and in my tests that was the memory cap. Only then do I ask whether the job needs more memory or has a leak. Raising the cap blindly hides a leak. A job that grows without bound will hit any limit eventually, so the cap only buys you time to fix the real cause.

Which numbers to pick

I do not have a formula. Start by watching what the job uses when it runs normally, then set MemoryMax with some room above that. A cap that is too tight kills healthy jobs, and the failure then looks like a flaky tool. When a job dies with signal 9, I check the cap first.

Put it in a wrapper

A shell function keeps the habit cheap.

capped() {
  systemd-run --user --scope -q -p MemoryHigh=2G -p MemoryMax=3G -p CPUWeight=50 "$@"
}

Then capped make test or capped ./backup.sh runs with limits. Choose the sizes for your own machine. The ones above are placeholders.

← All posts