Cgroups (control groups) is a Linux kernel feature that limits, accounts for, and isolates resource usage—such as CPU, memory, and disk I/O—for groups of processes. It organizes tasks into hierarchical structures so administrators and orchestration engines can enforce strict hardware quotas. While Linux namespaces isolate what a process can see, cgroups govern how much of the host system's resources that process is permitted to consume.
How it works
Administrators and container runtimes manage cgroups through a pseudo-filesystem typically mounted at /sys/fs/cgroup. Reading and writing plain text configuration files inside this directory tree adjusts resource ceilings in real time without requiring process restarts. The kernel enforces these boundaries using specialized subsystems called controllers:
cpu— regulates scheduler time, CPU weight, and hard bandwidth limits;memory— caps RAM and swap consumption, triggering targeted reclamation or termination when exceeded;io(formerlyblkio) — throttles read and write operations per second or byte throughput on block devices;pids— restricts the maximum number of tasks a group can spawn to prevent fork bombs.
Modern Linux distributions rely on cgroup v2, which consolidates all controllers into a single unified hierarchy. This design eliminates structural conflicts between separate controller trees and enables coordinated resource management across subsystems, such as accounting for disk writeback within a process group's memory limit.
Example
On systems using systemd, every service runs inside its own dedicated cgroup slice. You can inspect active resource allocation across the hierarchy with systemd-cgtop, or enforce strict limits on an existing service unit using systemctl set-property:
systemctl set-property nginx.service MemoryMax=2G CPUQuota=150%
This command immediately writes the corresponding limits to the service's cgroup directory, capping Nginx at two gigabytes of RAM and one and a half CPU cores. Container engines like Docker and Kubernetes perform the exact same kernel operations behind the scenes whenever you pass flags such as --memory or --cpus.
What matters when choosing a server or hosting
When evaluating infrastructure for containerized or multi-tenant workloads, verify that the host operating system defaults to the unified cgroup v2 hierarchy. Older container runtimes, legacy Java virtual machines, and outdated monitoring agents may misread memory limits under cgroup v2, so kernel and runtime compatibility must align.
Additionally, inspect whether your hosting provider exposes full cgroup control. Bare-metal servers and standard KVM virtual machines grant complete root access to /sys/fs/cgroup. In contrast, shared hosting environments or nested container platforms often restrict cgroup delegation, which can prevent you from running rootless containers or custom orchestration tools.
Common mistakes
- Omitting memory limits on containers: Leaving memory unbounded allows a single leaking application to exhaust host RAM, forcing the kernel's Out-Of-Memory (OOM) Killer to terminate critical system daemons or neighboring workloads.
- Confusing CPU shares with hard caps: CPU weights only govern contention when the processor is fully saturated, whereas a hard quota restricts execution even when idle cores are available on the host.
- Mixing legacy v1 and v2 tooling: Querying deprecated
/sys/fs/cgroup/memorypaths on a unified v2 host returns missing-file errors because controller files now reside directly inside each group's unified directory.
FAQ
What is the difference between cgroups and namespaces?
Namespaces provide visibility isolation—giving a process its own view of process IDs, network interfaces, and mount points—while cgroups enforce resource metering and limits on CPU, memory, and I/O consumption.
What happens when a process exceeds its cgroup memory limit?
The kernel first attempts to reclaim cached pages within that specific cgroup. If usage still exceeds the hard limit and swap is exhausted or disabled, the kernel invokes the cgroup-aware OOM Killer to terminate processes inside that group without crashing the wider host.
How can I check which cgroup version my server uses?
Run stat -fc %T /sys/fs/cgroup in a terminal. An output of cgroup2fs confirms the unified cgroup v2 hierarchy is active, whereas tmpfs indicates legacy cgroup v1 or hybrid mode.