</>EmbeddedInterview

How do you limit the CPU and memory used by processes on Linux?

MediumLinux System Programming

Short answer: Use nice/renice and scheduling policies to change CPU priority, ulimit/setrlimit() for per-process limits (address space, open files, core size, CPU seconds), and cgroups v2 for real resource control of groups of processes: cpu.max (hard CPU quota), cpu.weight (relative share), cpuset.cpus (which cores), memory.max/memory.high (memory limits), pids.max, and io.max. systemd exposes all of these as unit settings.

Priority

nice -n 10 ./batch_job       # lower priority (nice +10)
renice -n -5 -p 1234         # raise priority (needs root/CAP_SYS_NICE)
chrt -f 50 ./rt_task         # real-time FIFO priority 50
taskset -c 3 ./task          # restrict to CPU 3

Per-process resource limits (setrlimit)

Limit ulimit Effect
RLIMIT_AS -v maximum virtual address space; malloc/mmap fail beyond it
RLIMIT_DATA -d heap and data size
RLIMIT_STACK -s main thread stack size
RLIMIT_NOFILE -n open file descriptors
RLIMIT_CPU -t CPU seconds, then SIGXCPU/SIGKILL
RLIMIT_CORE -c core dump size
RLIMIT_NPROC -u processes per user
RLIMIT_MEMLOCK -l memory that may be locked with mlock

Each has a soft limit (what is enforced) and a hard limit (the ceiling an unprivileged process can raise its soft limit to).

cgroups v2 (what systemd uses)

mkdir /sys/fs/cgroup/camera
echo "50000 100000" > /sys/fs/cgroup/camera/cpu.max     # 50 ms CPU per 100 ms = half a core
echo 200M > /sys/fs/cgroup/camera/memory.max            # hard limit; OOM-kill inside the group
echo 150M > /sys/fs/cgroup/camera/memory.high           # throttle/reclaim above this
echo 2-3  > /sys/fs/cgroup/camera/cpuset.cpus
echo $PID > /sys/fs/cgroup/camera/cgroup.procs
# systemd unit
[Service]
CPUQuota=50%
MemoryMax=200M
MemoryHigh=150M
AllowedCPUs=2-3
TasksMax=64

Why cgroups instead of rlimits: rlimits apply per process (children get their own copy), so a service that forks many workers can still use unlimited total memory. A cgroup limits the sum for all processes in the group, and its accounting includes page cache and kernel memory.

Monitoring: systemd-cgtop, memory.current, memory.events (counts of OOM kills and throttling), cpu.stat (nr_throttled), and PSI files (cpu.pressure, memory.pressure).

Embedded use: keep the critical application responsive by limiting best-effort services (logging, updates, analytics) with cgroups, and give the main application a reserved core with cpuset.