Skip to content
BENIBIX Learn

Linux Control Groups (cgroups)

Infrastructure📦 ContainersContainer Basics

Also known as cgroups, cgroup, cgroups v2, Control Groups (Linux)

A Linux limit on memory and CPU. Each box gets its own cap.

Think of it like

Think of it like giving each child a set number of sweets. When they run out, they have to wait.

Example

Jack starts a container with a 512 MB memory cap. When his app grows past it, Linux stops the app instead of letting it slow the whole server.

Why it matters

With no limits, one busy box can take all the memory. Then every other program on the server slows down.

Definition

Control groups are a Linux feature that limits how much a group of programs may use. A cgroup can cap memory, CPU time and disk reads and writes. It also counts what the group is using, which is how tools show a container's usage. When a container goes over its memory cap, Linux stops a program inside it. Containers use cgroups so that one cannot starve the rest.

Example code

bash
docker run -d --memory=512m --cpus=1.5 nginx
These flags set a cap on memory and CPU for this one box. Linux keeps the cap.
The dashed fence around each container is its cgroup. It caps the memory and CPU the box can use.

Common mistake

Setting no memory limit and trusting the app. A leak then grows until the whole machine runs short.

Key takeaways

  1. 1
    Caps use. Memory, CPU and disk can be limited.
  2. 2
    Counts use. Tools read a container's numbers from it.
  3. 3
    Over the cap. A program past its memory cap is stopped.