GekBuzz

Answers, Explainers & Everyday Guides

How to Restrict CPU and Memory Usage on Linux Systems

How to Restrict CPU and Memory Usage on Linux Systems

Restricting CPU and memory usage on a Linux system helps you control resource allocation, prevent single processes from overwhelming your system, and maintain overall stability and performance. This is especially useful in multi-user environments, servers, or when running resource-intensive applications that must coexist without interfering with each other.

What You’ll Need

  • A Linux system with root or sudo access
  • Basic familiarity with the command line
  • Installed utility tools like cgroups or systemd (usually pre-installed on modern distributions)
  • Optional: cpulimit or nice commands for simpler CPU management

Step-by-Step

  1. Identify the process to limit. Use top, htop, or ps aux to find the PID or process name of the application consuming resources.
  2. Use cpulimit for simple CPU restriction. If you want to limit CPU usage of a process without complex setup, install cpulimit (e.g., sudo apt install cpulimit on Debian/Ubuntu). Then run:
    sudo cpulimit -p <PID> -l 30

    This limits the process to 30% CPU usage. It works only on running processes.

  3. Use nice and renice for CPU priority adjustment. These commands adjust process priority rather than absolute CPU usage. For example:
    nice -n 19 <command>

    runs a command with the lowest priority.
    To change priority of a running process:

    sudo renice 19 -p <PID>

    This reduces CPU scheduling priority but does not strictly cap CPU usage.

  4. Use cgroups v2 to limit CPU and memory. Modern Linux systems use cgroups (control groups) to manage resources. First, verify cgroups v2 is enabled:
    mount | grep cgroup2

    If not mounted, mount it:

    sudo mount -t cgroup2 none /sys/fs/cgroup

    Create a new cgroup for your process:

    sudo mkdir /sys/fs/cgroup/mylimit

    Limit CPU: To limit CPU usage, write a quota and period:

        echo 100000 > /sys/fs/cgroup/mylimit/cpu.max
        echo 100000 > /sys/fs/cgroup/mylimit/cpu.max
        

    For example, to limit to 20% CPU on a single CPU:

        echo "20000 100000" | sudo tee /sys/fs/cgroup/mylimit/cpu.max
        

    Limit memory: To limit memory, write the maximum bytes:

    echo 500M | sudo tee /sys/fs/cgroup/mylimit/memory.max

    Add your process to this cgroup:

    echo <PID> | sudo tee /sys/fs/cgroup/mylimit/cgroup.procs
  5. Use systemd slices for persistent resource limits. If your system uses systemd, create a slice with resource limits:

    Create a file /etc/systemd/system/mylimit.slice with contents:

        [Slice]
        CPUQuota=20%
        MemoryMax=500M
        

    Reload systemd:

    sudo systemctl daemon-reload

    Start a service or scope in this slice:

    systemd-run --slice=mylimit.slice --scope <command>
  6. Verify limits are applied. Use top, htop, or cat /sys/fs/cgroup/mylimit/cpu.max and memory.max to confirm settings.
  7. Adjust limits as needed. Modify the quota or memory.max values or add/remove processes from the cgroup or slice.

Common Mistakes

  • Trying to limit CPU with nice or renice expecting a hard cap — these only adjust priority, not usage limits.
  • Not running commands with sufficient privileges (root or sudo) to modify cgroups or systemd slices.
  • Confusing cgroups v1 with v2 syntax and mount points; cgroups v2 uses unified hierarchy and different file names.
  • Setting resource limits that are too low, causing processes to crash or behave unexpectedly.
  • Not verifying if cgroups v2 is enabled or mounted before attempting to use it.
  • Failing to add the correct PID to the cgroup after creation, so limits don’t apply.

Tips

  • Use htop or top to monitor CPU and memory usage live while testing limits.
  • For persistent limits on services, prefer systemd slices over manual cgroup manipulation.
  • Use relative CPU quotas based on the number of CPUs (e.g., for 4 CPUs, 20% means 80000/100000 * 4).
  • Combine CPU and memory limits to avoid runaway processes consuming all resources.
  • Use cpulimit for quick, temporary CPU limits on individual processes without systemd or cgroups setup.
  • Document your limit configurations for future reference and troubleshooting.

Troubleshooting

If your CPU or memory limits don’t seem to work as expected, try the following:

  • Check permissions: Ensure you have root or sudo privileges to create and modify cgroups or systemd slices.
  • Verify cgroups version: Confirm if your system uses cgroups v1 or v2, and use the appropriate commands and paths.
  • Confirm cgroup mount points: Use mount | grep cgroup to verify cgroups are mounted correctly.
  • Ensure correct PID added: Double-check you’ve added the correct process ID to the cgroup or systemd slice.
  • Check for conflicting limits: Other system services or container runtimes may impose limits that interfere.
  • Review logs: Look at journalctl and system logs for error messages related to cgroups or systemd.
  • Test with simpler tools: Try cpulimit or nice to isolate whether the problem is with your cgroup setup or the process itself.
  • Restart systemd or reboot: Changes in systemd slices sometimes require a daemon reload or system reboot to take full effect.

Frequently Asked Questions

Can I limit CPU usage for all users on a system?

Yes, by configuring systemd slices or cgroups at the system level, you can impose limits on all processes belonging to specific users or groups.

What is the difference between nice and cpulimit?

nice adjusts CPU scheduling priority but does not limit CPU usage, while cpulimit enforces a hard CPU usage cap on a process.

Are cgroups compatible with all Linux distributions?

Most modern Linux distributions support cgroups v1 and/or v2 by default, but some older or minimalist distros might require manual setup.

Can I limit memory usage to prevent out-of-memory kills?

Yes, setting memory limits via cgroups or systemd slices prevents processes from exceeding specified memory, reducing the risk of system-wide OOM kills.

How do I check which cgroup version my system uses?

Run mount | grep cgroup. If you see a unified cgroup2 mount, your system uses cgroups v2; otherwise, it uses v1.

Will limiting CPU slow down my process?

Yes, limiting CPU usage restricts the processing power available, which may slow down execution but helps maintain system responsiveness and fairness.

By following this guide, you can effectively control CPU and memory usage on your Linux system, ensuring better stability and resource distribution. Experiment with different methods to find the best fit for your environment and workload.

Related

How to Restrict CPU and Memory Usage on Linux Systems

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top