How To Limit CPU Usage On Ubuntu 24.04 or Newer
Managing CPU usage is crucial for maintaining the performance and efficiency of your applications and services, especially on servers or systems handling multiple tasks. In this blog post, we will explore various methods to limit CPU usage on Ubuntu 24.04 and newer versions, ensuring that your processes run smoothly without hogging system resources.
Why Limit CPU Usage?
Limiting CPU usage is essential for several reasons:
- Preventing Resource Starvation: When one process consumes too much CPU, it can starve other processes of the resources they need to function correctly.
- Improving System Stability: High CPU usage can lead to system overheating, crashes, or slowdowns. By managing CPU loads, you enhance overall system stability.
- Optimizing Performance: By balancing CPU usage across processes, you can improve the performance and responsiveness of applications.
Methods to Limit CPU Usage
1. Using cpulimit
cpulimit
is a command-line utility that allows you to limit the CPU usage of a specific process. Here’s how to install and use it:
Installation
Open your terminal and run:
sudo apt update
sudo apt install cpulimit
Usage
To limit the CPU usage of a process, you need to know its PID (Process ID). You can find the PID using the ps
command:
ps aux | grep your_process_name
Once you have the PID, you can limit its CPU usage. For example, to limit a process to 50% CPU:
sudo cpulimit -p <PID> -l 50
Replace <PID>
with the actual process ID.
2. Using nice
and renice
The nice
command allows you to start a process with a specified priority, while renice
lets you change the priority of an already running process.
Starting a Process with nice
You can start a new process with a lower priority (higher nice value) like this:
nice -n 10 your_command
The range for nice values is from -20 (highest priority) to 19 (lowest priority). The default value is 0.
Changing the Priority of a Running Process with renice
To change the priority of an existing process, use:
sudo renice -n 10 -p <PID>
3. Using systemd
Resource Control
If you are running services managed by systemd
, you can set resource limits directly in the service configuration.
Editing the Service File
- Locate the service file, usually found in
/etc/systemd/system/
or/lib/systemd/system/
. - Open the service file in a text editor:
sudo nano /etc/systemd/system/your_service.service
- Add the following lines under the
[Service]
section to limit the CPU usage:
[Service]
CPUQuota=50%
This example limits the service to 50% CPU usage.
- Save the changes and exit the editor.
- Reload the systemd configuration and restart the service:
sudo systemctl daemon-reload
sudo systemctl restart your_service
4. Using cgroups
Control Groups (cgroups) is a Linux kernel feature that allows you to allocate, limit, and prioritize resources, including CPU.
Creating a Cgroup
- Install
cgroup-tools
:
sudo apt install cgroup-tools
- Create a new cgroup:
sudo cgcreate -g cpu:/mygroup
- Set the CPU limit (e.g., to 50%):
echo 50000 | sudo tee /sys/fs/cgroup/cpu/mygroup/cpu.cfs_quota_us
- Add a process to the cgroup:
sudo cgclassify -g cpu:mygroup <PID>
Conclusion
Limiting CPU usage on Ubuntu 24.04 or newer can significantly improve system performance, stability, and resource management. Whether you choose to use cpulimit
, nice
, systemd
, or cgroups
, each method offers flexibility depending on your specific needs.
By implementing these techniques, you can ensure that your applications and services run efficiently while maintaining a responsive system. If you have any questions or need further assistance, feel free to reach out to our support team at Greenhost.cloud!