Cgroups v2 - limit memory and CPU usage for all users on Linux

Posted on Thu 01 February 2024 by Pavlo Khmel

Starting with systemd v239 it is more easy to set cgroups limits for all users on a system.

This is an example of setting a memory limit of 30GB and CPU usage equivalent to 8 x 100% loaded CPUs:

# mkdir -p /etc/systemd/system/user-.slice.d

# cat /etc/systemd/system/user-.slice.d/50-memory.conf

# systemctl daemon-reload

NOTE: Use values > 100% for allotting CPU time on more than one CPU.

Limits will be applied immediately.

Check applied limits. Get any logged-in user UID with "id" command.

$ cat /sys/fs/cgroup/user.slice/user-[UID].slice/memory.max

$ cat /sys/fs/cgroup/user.slice/user-[UID].slice/cpu.max
800000 100000

Useful links: