Managing CPU and memory usage is essential for maintaining system stability, especially on VPS and servers running multiple applications. Modern Linux distributions use systemd with cgroups v2 to enforce resource limits in a reliable and persistent way. This guide explains the recommended approach using systemd, along with alternative tools for temporary or lightweight control.
Why Limit CPU and Memory Usage?
Limiting resource usage helps prevent a single process or service from consuming excessive CPU or RAM. This ensures consistent performance for critical applications, reduces the risk of system slowdowns or crashes, and improves overall server stability, particularly in shared or production environments.
Method 1: Limit CPU and Memory Using systemd (Recommended)
On modern Linux systems (Ubuntu 22.04+, AlmaLinux 9, Rocky Linux 9), systemd manages cgroups v2 automatically. Setting limits at the service level is the most reliable and persistent method.
Note: Ubuntu 20.04 uses cgroups v1 by default. While systemd-based limits still work, full cgroups v2 functionality is available only on Ubuntu 22.04 and later.
Step 1: Identify the systemd service
First, determine the service you want to restrict. For example:
systemctl status nginx
Step 2: Edit the service override file
Use the following command to create or edit a systemd override:
sudo systemctl edit nginx
Step 3: Set CPU and memory limits
Add the resource limits under the [Service] section:
[Service] MemoryMax=500M CPUQuota=30%
- MemoryMax limits the maximum RAM the service can use
- CPUQuota restricts CPU usage as a percentage of total CPU time
Save and exit the editor.
Step 4: Reload and apply changes
Reload systemd and restart the service:
sudo systemctl daemon-reexec sudo systemctl restart nginx
These limits are persistent across reboots and enforced at the kernel level.
Method 2: Limit CPU Usage Using cpulimit (Temporary)
cpulimit restricts CPU usage by pausing and resuming a process. It is useful for short-term control but is not persistent and does not enforce memory limits.
Install cpulimit
Ubuntu/Debian:
sudo apt install cpulimit
CentOS/RHEL (EPEL required):
sudo yum install epel-release sudo yum install cpulimit
Apply a CPU limit
Limit an existing process to 30% CPU:
sudo cpulimit -p <PID> -l 30
This method is suitable for temporary troubleshooting or non-critical workloads.
Method 3: Adjust CPU Priority Using nice and renice
The nice and renice commands adjust scheduling priority but do not enforce strict CPU limits. They only influence how often a process gets CPU time.
Start a process with lower priority:
nice -n 10 /path/to/program
Change the priority of a running process:
sudo renice 15 -p <PID>
This approach is best used for background or low-priority tasks.
Verify Resource Usage
To monitor CPU and memory consumption:
top
For a more user-friendly view:
sudo apt install htop htop
To check systemd-applied limits:
systemctl show nginx | grep -E 'MemoryMax|CPUQuota'
Best Practices
- Use systemd resource limits for production environments
- Apply limits before deploying high-load applications
- Avoid restricting critical system services
- Monitor usage regularly with htop or top
- Use temporary tools like cpulimit only when persistence is not required
Limiting CPU and memory usage is essential for maintaining a stable and efficient Linux server. While tools like cpulimit and nice are useful for temporary control, systemd-based resource limits provide the most reliable and persistent solution on modern Linux systems. By using the appropriate method, administrators can prevent resource abuse and ensure consistent performance across applications.
A Linux VPS Hosting solution provides dedicated CPU and RAM resources, root access and the flexibility to optimise application performance.
Want to manage CPU usage on Windows too? Learn How to Prevent CPU Throttling in Windows