Linux is a powerful and efficient operating system, but high RAM and CPU usage can degrade performance, slow down applications, and even cause system crashes when you’re running a server, workstation, or embedded system, and optimizing resource usage is essential for smooth operation.

In this guide, we’ll explore practical methods to reduce RAM and CPU usage on Linux. We’ll cover monitoring tools, process management, kernel tweaks, and system optimization techniques to keep your system running efficiently.

1. Identify Resource Hungry Processes

The first step in reducing RAM and CPU usage is identifying which processes are consuming the most resources by utilizing several command-line tools:

a. Using top Command

The top command provides a real-time view of system processes, including CPU and memory usage.

top
  • Press P to sort processes by CPU usage.
  • Press M to sort processes by memory usage.
  • Look for processes consuming excessive resources and note their Process ID (PID).
Real-Time System Process Monitoring
Real-Time System Process Monitoring

b. Using htop Command

htop is an interactive process viewer that provides a more user-friendly interface than top.

To install htop on Linux, use the following appropriate command for your specific Linux distribution.

sudo apt install htop         [On Debian, Ubuntu and Mint]
sudo dnf install htop         [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/htop  [On Gentoo Linux]
sudo apk add htop             [On Alpine Linux]
sudo pacman -S htop           [On Arch Linux]
sudo zypper install htop      [On OpenSUSE]    
sudo pkg install htop         [On FreeBSD]

Run htop to view and manage processes easily.

htop
  • Press F6 to sort processes by CPU or memory usage.
  • Press F9 to kill a selected process.
htop - Interactive Process Viewer
htop – Interactive Process Viewer

c. Using ps Command

The ps command can be used to list processes and their resource usage.

ps aux --sort=-%mem | head  # Top memory-consuming processes
ps aux --sort=-%cpu | head  # Top CPU-consuming processes
Check Memory Consuming Processes
Check Memory Consuming Processes

2. Kill Unnecessary Processes

Once you’ve identified resource-hogging processes, you can terminate them to free up resources using the kill command followed by the PID.

kill PID

If the process doesn’t terminate, use the -9 flag to force-kill it.

kill -9 PID

Alternatively, use the pkill command to kill processes by name.

pkill process_name

3. Optimize Startup Applications

Many applications and services start automatically at boot, consuming valuable system resources.

To manage startup applications:

a. Using systemctl Command

List all enabled services using the systemctl command.

systemctl list-unit-files --type=service | grep enabled

Disable unnecessary services.

sudo systemctl disable service_name
List Enabled Services
List Enabled Services

b. Using GUI Tools

If you’re using a desktop environment like GNOME or KDE, use the built-in startup applications manager to disable unnecessary programs.

4. Reduce Swappiness (Optimize Swap Usage)

Swappiness controls how often the system uses swap space instead of RAM. A high swappiness value can lead to excessive swapping, slowing down your system.

To check the current swappiness value:

cat /proc/sys/vm/swappiness

The default value is usually 60, but to reduce swappiness you need to edit the /etc/sysctl.conf file.

sudo nano /etc/sysctl.conf

Add or modify the following line.

vm.swappiness=10

Save the file and apply the changes.

sudo sysctl -p

5. Use Lightweight Desktop Alternatives

Heavy desktop environments and applications can strain system resources, so consider switching to lightweight alternatives:

a. Desktop Environments

Replace GNOME or KDE with lighter options like XFCE, LXDE, or MATE.

b. Applications

Use lightweight applications such as:

  • Text Editor: Replace LibreOffice with AbiWord or Mousepad.
  • Web Browser: Use Firefox with fewer extensions or switch to lightweight browsers like Midori.
  • File Manager: Replace Nautilus with Thunar or PCManFM.

6. Clear Cache and Buffers

Linux uses RAM to cache files and improve performance. However, you can manually clear the cache if needed:

sudo sync; sudo sysctl -w vm.drop_caches=3

This command clears pagecache, dentries, and inodes. Use it cautiously, as it may temporarily slow down file access.

7. Optimize Kernel Parameters

To tune or optimize kernel parameters, you need to edit /etc/sysctl.conf file and add the following or adjust parameters to improve system performance.

vm.dirty_background_ratio=5
vm.dirty_ratio=10
vm.min_free_kbytes=65536
kernel.sched_autogroup_enabled=0

These settings control how often dirty data is written to disk, reducing RAM usage.

8. Reduce Memory Usage with ZRAM

Zram and Zswap are compressed memory technologies that can reduce RAM usage.

sudo apt install zram-config 
sudo systemctl start zram-config

Once installed, you need to edit the GRUB configuration file (/etc/default/grub) and add:

GRUB_CMDLINE_LINUX_DEFAULT="zswap.enabled=1"

Update GRUB and reboot:

sudo update-grub
sudo reboot

9. Limit CPU Usage of Processes

If a process is hogging the CPU, limit its usage using cpulimit.

sudo apt install cpulimit  # Debian/Ubuntu  
sudo yum install cpulimit  # RHEL/CentOS  

Limit a process (e.g., firefox) to 30% CPU usage.

sudo cpulimit -e firefox -l 30

To permanently limit a process, use systemd.

sudo systemctl set-property <service_name> CPUQuota=30%

10. Regularly Update Your System

Keeping your system updated ensures you have the latest performance improvements and bug fixes.

sudo apt update && sudo apt upgrade  # For Debian/Ubuntu
sudo yum update  # For CentOS/RHEL

Troubleshooting Tips

  • If a process doesn’t terminate with kill, use kill -9 PID to force-kill it.
  • If zram or zswap doesn’t work, ensure your kernel supports these features.
  • If system performance doesn’t improve, consider upgrading your hardware (e.g., adding more RAM or switching to an SSD).
Conclusion

Optimizing RAM and CPU usage on Linux is a blend of monitoring, tweaking, and occasionally upgrading hardware. By identifying resource-hogging processes, adjusting system settings, and opting for lightweight alternatives, you can breathe new life into your system.

Have you tried these optimization techniques? Share your experience in the comments below or visit Tecmint.com for more Linux guides and tutorials.

Similar Posts