Controlling CPU, RAM, and I/O on Fedora with cgroups v2 and systemd

Fedora tutorial - IT technology blog
Fedora tutorial - IT technology blog

You have your IDE open, a few Docker containers running, and you kick off a project build. A few seconds later, your mouse cursor freezes. The laptop fans spin up to full speed, and the entire system locks up because the compiler just ate all 16GB of RAM.

I have been using Fedora as my daily driver for over two years. The fast package updates are great, but when I first switched, my machine would freeze constantly whenever I ran cargo build or compressed large files. The permanent fix is to use cgroups v2 together with systemd to set resource quotas for individual processes.

Quick start: Limit resources in 5 minutes

Fedora has enabled cgroups v2 by default since Fedora 31 (back in 2019), so you don’t need to install any extra packages. Open your Terminal and run a quick check:

stat -fc %T /sys/fs/cgroup/

If the output is cgroup2fs, your system is ready to go.

Now, try running a Python loop that normally consumes 100% CPU, but cap it at a maximum of 50% of one core and 256MB of RAM:

systemd-run --user --scope -p CPUQuota=50% -p MemoryMax=256M python3 -c "while True: pass"

Open another Terminal tab to monitor resource usage in real time:

systemd-cgtop

Look at the CPU column: the Python process is pinned right around 50% instead of maxing out a full core at 100% like usual. Press Ctrl + C in both tabs to stop the test. That’s it—just one command without touching a single config file.

How do cgroups v2 and systemd work?

cgroups (Control Groups) is a Linux kernel feature for allocating and limiting resources for groups of processes. You can set ceilings on CPU, RAM, or disk bandwidth for a specific application, ensuring that a buggy or overloaded task doesn’t bring down the whole machine.

Previously, cgroups v1 split controllers (CPU, memory, I/O) into separate directory hierarchies, which made conflicts all too common. With cgroups v2, the Linux kernel unifies everything into a single hierarchy under /sys/fs/cgroup. This lets the kernel account accurately when a process is both reading/writing to disk and consuming page cache.

3 systemd unit types you need to know

Instead of writing directly to virtual files in /sys/fs/cgroup, on Fedora you interact through systemd. You only need to remember these 3 concepts:

  • Service (.service): Background services such as Nginx, PostgreSQL, or your worker app.
  • Scope (.scope): Groups of processes launched from outside systemd, such as the systemd-run --scope command we just ran.
  • Slice (.slice): A parent unit used to group multiple services or scopes into a single bucket and apply resource limits to the entire group.

5 most commonly used resource limit properties

When configuring cgroups v2 via systemd, you will see these 5 properties over and over again:

  • CPUQuota=50%: A hard cap on CPU time, where 100% equals one core. On an 8-core machine, setting 200% allows using up to 2 cores.
  • CPUWeight=100: Priority under CPU contention (ranging from 1 to 10000, default is 100). When the CPU is idle, a process can still run at full speed; only when the system is overloaded do processes with higher weights get priority.
  • MemoryHigh=512M: Soft limit. Once reached, the kernel throttles the process and aggressively reclaims page cache so the application backs off.
  • MemoryMax=1G: Hard limit. If the process exceeds 1GB of RAM, the OOM Killer terminates only the processes inside this cgroup, leaving the rest of the system untouched.
  • IOReadBandwidthMax and IOWriteBandwidthMax: Per-device read/write speed limits, for example IOWriteBandwidthMax=/dev/nvme0n1 50M to cap writes at 50MB/s.

Applying permanent limits to a Service and creating a custom Slice

The systemd-run command only applies to the current session. For long-running services, you should use a drop-in override to persist the configuration. This ensures your settings won’t be overwritten during package updates.

Suppose you want to set limits for worker-app.service. Run the following command to open the configuration editor:

sudo systemctl edit worker-app.service

Add the following snippet at the top of the file (above the instructional comment lines):

[Service]
CPUQuota=150%
MemoryHigh=800M
MemoryMax=1G
IOAccounting=true
IOWriteBandwidthMax=/dev/nvme0n1 30M

Save and exit the editor; systemctl edit automatically reloads the unit file for you. Next, restart the service to apply the new limits:

sudo systemctl restart worker-app.service

Verify that the properties took effect:

systemctl show worker-app.service -p CPUQuotaPerSecUSec -p MemoryHigh -p MemoryMax

The output will look like CPUQuotaPerSecUSec=1.500000s, MemoryHigh=838860800 (bytes), and MemoryMax=1073741824 (bytes).

Grouping multiple services into a single Slice

Sometimes you run 3–4 workers at once and want their combined RAM usage to stay under 2GB, rather than giving each one a rigid 512MB cap. Creating a custom .slice solves this cleanly.

Create the file /etc/systemd/system/heavy-jobs.slice with the following contents:

[Unit]
Description=Resource-limited heavy job group
Before=slices.target

[Slice]
CPUQuota=200%
MemoryHigh=1500M
MemoryMax=2G

For each service you want to include, run sudo systemctl edit <service-name> and assign it to the new slice:

[Service]
Slice=heavy-jobs.slice

Since you created the .slice file manually, run sudo systemctl daemon-reload and then restart the affected services. From now on, the entire group shares a 2GB RAM and 200% CPU budget without starving other applications.

Practical tips from using Fedora daily

  1. Keep MemoryHigh at 75%–80% of MemoryMax: This buffer gives the kernel enough room to throttle the process before having to invoke the OOM Killer.
  2. Don’t blindly copy disk device names: Many tutorials hardcode /dev/sda, but modern laptops typically use /dev/nvme0n1 and VMs use /dev/vda. Run lsblk first to find the exact device name on your machine.
  3. Wrap heavy commands with systemd-run: When compressing 30–40GB archives with tar or rendering video with ffmpeg, I usually prefix them with systemd-run --user --scope -p CPUQuota=400%. That leaves plenty of headroom on an 8-core machine for coding or smooth video calls.
  4. Leverage the built-in systemd-oomd: Since Fedora 34, systemd-oomd has been enabled by default. It reads Pressure Stall Information (PSI) metrics from cgroups v2 to proactively kill memory-leaking processes before your desktop freezes solid.

Next time you are about to run a heavy build or a memory-hungry script on Fedora, try wrapping it with systemd-run first. Once you’re comfortable with the numbers, move those settings into systemctl edit for your critical background services.

Share: