Linux cgroups v2: Hierarchical Resource Control
Linux cgroups v2 are a kernel mechanism for grouping processes and accounting for and controlling the resources they use. A cgroup, short for control group, is a node in a hierarchy: processes belong to a node, and child nodes inherit the resource accounting of their ancestors.
Container runtimes use cgroups to make statements such as “this container may use at most two CPUs, four GiB of memory, and 256 processes” enforceable by the kernel rather than by cooperation from the application.
The problem cgroups solve
Without cgroups, ordinary Linux processes compete in one system-wide pool. The CPU scheduler—the kernel component that chooses which runnable process executes—can share CPU time fairly, but it does not naturally know that ten processes belong to one service and two belong to another. A memory-hungry workload can consume most available RAM, and a process that keeps forking can exhaust the system’s process table.
A runtime could track the processes it started, but tracking alone does not enforce limits. Processes can fork, change identities, or crash while the runtime is watching them. The kernel is the authority that allocates CPU, charges memory, and creates processes, so cgroups put the policy at that layer.
Cgroups are not containers by themselves. Namespaces provide much of a container’s separate view of processes, filesystems, networks, and other system objects. A cgroup supplies resource accounting and enforcement; container runtimes normally use both.
One hierarchy, several controllers
Cgroups v2 uses a single, unified hierarchy mounted by the operating system. Each directory in that hierarchy represents a cgroup. The directory contains kernel-provided files rather than ordinary application data. Writing a value to one of those files changes the policy for that group.
The main resource controls are called controllers. A controller is a kernel subsystem that applies cgroup policy to one resource class. Common v2 controllers include:
cpufor CPU distribution and quotasmemoryfor memory accounting and limitspidsfor limiting the number of processes
A cgroup can contain child cgroups. If a service cgroup has separate api and worker children, the parent can impose an overall budget while the children divide that budget further. Resource usage is charged through the hierarchy: a child’s CPU time and memory contribute to its ancestors’ totals.
This makes limits composable. For example, a container can have a 4 GiB memory limit while a parent representing a whole application has a 12 GiB limit. The container cannot exceed 4 GiB, and the application’s containers together cannot exceed 12 GiB.
The cgroup.procs file identifies the processes in a cgroup and can be used to move processes between groups. A process created by a process already in a cgroup normally starts in that same cgroup. This inheritance is important: a worker that a container process forks is covered without the runtime having to discover it afterward.
CPU: sharing time and imposing a ceiling
The CPU controller has two related jobs. Weight determines how CPU time is divided when groups compete. Quota imposes an upper bound over a period.
In cgroups v2, cpu.weight is a relative priority, normally ranging from 1 to 10,000. If two busy sibling groups have weights of 100 and 200, the second is eligible for roughly twice as much CPU time under contention. Weight does not guarantee that a group will use CPU when no other work is competing.
cpu.max expresses a quota and period as two values:
200000 100000
This permits 200,000 microseconds of CPU time during each 100,000-microsecond period—effectively two CPUs’ worth of execution for a group whose processes can run in parallel. The special value max removes the quota, so max 100000 means no CPU ceiling but still specifies the period.
When a group exhausts its quota, the kernel throttles its runnable processes until the next period. They are not killed; they simply cannot run during the throttled interval. cpu.stat reports usage and throttling counters, which helps explain why an application is slow even though its processes remain healthy.
Memory: charging pages and handling pressure
The memory controller charges a cgroup for memory used by its processes, including memory such as anonymous application allocations and filesystem-backed pages. Shared pages are accounted according to kernel rules, so the number is a resource charge rather than a simple sum of every process’s virtual address space.
Useful v2 files include:
memory.current, the current charge, including descendantsmemory.max, a hard limitmemory.high, a pressure threshold that makes reclaim more aggressive and slows the groupmemory.events, counters for conditions such as high-threshold crossings and limit breaches
memory.max is an enforcement boundary. When the kernel cannot reclaim enough memory to keep a cgroup below it, it invokes the out-of-memory procedure for that cgroup. The kernel may terminate one or more processes, commonly described as an OOM kill, rather than allowing the group to consume unlimited RAM. A container runtime or orchestrator often surfaces this as an “out of memory” exit.
memory.high is intentionally softer. Crossing it does not normally kill processes; it creates memory pressure and can throttle allocation while the kernel reclaims pages. This gives an operator a way to slow a workload before reaching the fatal limit. Swap has separate controls when swap accounting is available, including memory.swap.max.
Process counts
The pids controller limits the number of processes in a cgroup and its descendants with pids.max. This is a count limit, not a CPU or memory limit. It prevents a fork bomb—a process that repeatedly creates children—from exhausting the host’s process capacity.
The kernel rejects a new process creation when the limit would be exceeded. Because the check occurs during process creation, a program cannot evade the limit simply by forking faster than a runtime can observe it. A process may therefore receive a fork-related failure even when plenty of CPU and memory remain.
How a container runtime uses cgroups
When starting a container, a runtime creates a cgroup beneath a runtime- or system-managed parent, enables the needed controllers for that subtree, and writes policies such as CPU quota, memory limit, and process maximum. It then places the container’s initial process in the cgroup before that process creates application children.
Flags such as Docker’s --cpus, --memory, and --pids-limit, or equivalent settings in an orchestrator, are usually translated into these kernel files. The runtime later reads files such as cpu.stat, memory.current, and memory.events to report usage and diagnose throttling or OOM events.
The hierarchy also protects the host’s policy boundaries. A runtime can manage a delegated subtree without being allowed to rewrite unrelated cgroups. Parent limits still apply, so a container cannot raise its own effective budget beyond the limits imposed above it.
That is the central model: processes live in a tree, resource usage flows up the tree, and each controller enforces policy at every relevant level. Containers add isolation around that model, while cgroups provide the kernel-enforced accounting and limits that make workload resource controls real.