Linux / OS Fundamentals Series

⚖️ A Top 1% Hands-On for Building Linux cgroups (Control Groups) Yourself and Experiencing the Other Half of a 'Container'

Introduction

This article is part of the Top 1% Series Complete Article Guide, and article 19.1 in the Linux Infrastructure Series. You can follow along with one of the Ubuntu Server environments set up in the Hands-On Prep Manual.

Prerequisite Knowledge

Getting the Big Picture

As confirmed in the namespace hands-on, a namespace only restricts the information visible to a process (the list of other processes, mount state, and similar) — it places no limit whatsoever on how much CPU or memory it may consume. In other words, a namespace alone leaves open the possibility that a process that thinks it’s isolated could still exhaust the entire host’s resources.

graph TB
    subgraph Namespace["What a Namespace Restricts"]
        A1["Visible information (viewpoint)<br/>process list, mount state, and similar"]
    end
    subgraph Cgroups["What cgroups Restricts"]
        B1["The amount of resource consumable<br/>CPU time, memory capacity, and similar"]
    end
    Namespace -.only combined together.-> Container["the unit called a 'container'"]
    Cgroups -.only combined together.-> Container

Hands-On Steps

Step 1: Run a Process That Exhausts Memory, With No Limit at All

First, deliberately set up a process that allocates a large amount of memory, with no limit in place.

python3 -c "
data = []
while True:
    data.append(' ' * 10**8)  # allocate about 100MB at a time
    print(f'{len(data) * 100}MB allocated')
" &
MEMHOG_PID=$!
sleep 3
kill $MEMHOG_PID

With no limit at all in this state, as long as it keeps allocating memory, the host’s overall memory keeps getting squeezed.

Step 2: Create a cgroup and Set a Memory Limit

Using cgroups v2 (the currently dominant interface), create a new cgroup.

sudo mkdir /sys/fs/cgroup/memhog-demo
echo "100M" | sudo tee /sys/fs/cgroup/memhog-demo/memory.max

Simply writing 100M into the file memory.max sets a 100MB cap on the total memory every process belonging to this cgroup can consume. This is the same underlying idea as procfs presenting the kernel’s own settings as “live files” — cgroups configuration, too, is done through ordinary file reads and writes.

Step 3: Add a Process to This cgroup and Confirm What Happens at the Limit

echo $$ | sudo tee /sys/fs/cgroup/memhog-demo/cgroup.procs
python3 -c "
data = []
while True:
    data.append(' ' * 10**8)
    print(f'{len(data) * 100}MB allocated')
"

Output (illustrative):

100MB allocated
Killed

The instant memory allocation tried to exceed 100MB, this process got forcibly terminated (an OOM Kill). Writing your own process ID ($$) into cgroup.procs places the current shell and its child processes under this cgroup’s limit. You’ve confirmed that, as an entirely independent mechanism from a namespace’s viewpoint restriction, actual resource consumption itself gets forcibly cut off by the kernel.

How This Differs From an Ordinary Out-of-Memory Condition (a Host-Wide OOM)

Normally, when the host’s overall physical memory runs low, the kernel selects an OOM Kill target based on criteria like which process across the entire system is consuming the most memory. With a cgroup’s memory.max set, by contrast, the instant that cgroup itself exceeds its own assigned limit, only the processes inside that cgroup become targets. The decisive difference is that even while the host overall still has plenty of free memory, the forced termination happens purely because this cgroup exceeded the limit assigned to it — an arbitrary, agreed-upon ceiling.

Step 4: Set a Limit on CPU Usage Too

sudo mkdir -p /sys/fs/cgroup/cpuhog-demo
echo "50000 1000000" | sudo tee /sys/fs/cgroup/cpuhog-demo/cpu.max
echo $$ | sudo tee /sys/fs/cgroup/cpuhog-demo/cgroup.procs
yes > /dev/null &
sleep 5
top -b -n 1 | grep yes

Setting cpu.max to "50000 1000000" means: “out of every 1,000,000-microsecond (1-second) period, only allow 50,000 microseconds (0.05 seconds) of CPU time usage.” This lets you confirm that the yes command, which would normally occupy an entire CPU core at 100%, ends up capped at roughly 5% CPU usage.

What a Pro Sees Here (Top 1% Understanding)

“Namespace + cgroups” Is a Concrete Example of the Design Philosophy “Use a Separate Tool for Each Kind of Problem”

The biggest discovery you can confirm through this hands-on, together with the namespace hands-on, is that the requirement “isolation” — which looks like a single purpose at a glance — actually breaks down into two entirely independent problems: “what’s visible” (a namespace) and “how much can be used” (cgroups). What a tool like Docker provides as the single concept “a container” is really nothing more than automating operations against these two entirely separate mechanisms, bundled together. A top-1% engineer, whenever encountering a new, abstracted concept, has the habit of always breaking it down and asking: “what independent mechanisms is this actually a combination of?”

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. A process inside a container suddenly terminates, leaving only a “Killed” message: First check whether that container’s assigned memory limit (a setting equivalent to memory.max) got exceeded.
  2. A specific process’s CPU usage plateaus lower than expected: Check whether the cgroup that process belongs to has a cpu.max limit configured.
  3. You try to create a cgroup, but the file can’t be found: cgroups v1 and v2 have different interfaces. Check /sys/fs/cgroup’s structure to confirm which version is actually running.

Summary

Takeaways to Apply Today

  1. When you hit an incident related to a container’s resource usage, first check the cgroups limit configured for that container.
  2. Whenever you encounter a new, abstracted concept, build the habit of breaking it down and asking what independent mechanisms it’s actually a combination of.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).