How to Reduce RAM & CPU Usage on Linux

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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

12 Comments

Leave a Reply
  1. I have just replaced my old desktop with a mini. Incredible improvement in performance. Makes life bearable again and frees up lots of floor space.

    Reply
  2. Atop is an ASCII full-screen performance monitor for Linux that is capable of reporting the activity of all processes (even if processes have finished during the interval), daily logging of system and process activity for long-term analysis, highlighting overloaded system resources by using colors, etcetera.

    Have a look on Github, for me, it’s by far the best one as regards Linux System monitoring.

    https://github.com/Atoptool/atop

    Reply
    • @Ajrun,

      htop is great, but there are definitely more options to explore. Let me know if you try any new ones and how they work for you!

      Reply
  3. Thanks for this detailed guide!

    I manage multiple Linux servers, and htop + cpulimit have been game-changers for controlling runaway processes. One thing I’d add is using nice and ionice for background tasks to prevent CPU hogging.

    Would love to see a section on that!

    Reply
    • htop and cpulimit are great, and you’re right that nice and ionice are essential for managing background tasks without affecting system performance.

      For CPU priority:

      nice -n 10 command_name
      

      For I/O priority:

      ionice -c 2 -n 7 command_name
      
      Reply
  4. Great article!

    I applied the swappiness tweak and disabled a few unnecessary services and my Linux laptop feels much improved now. Do you have any recommendations for reducing Firefox’s RAM usage further? It’s still eating up a lot of memory even with extensions disabled

    Reply
    • Glad to hear the tweaks helped! Firefox can still be a memory hog, try these:

      Disable disk cache (about:configbrowser.cache.disk.enable → false).
      Limit content processes (about:configdom.ipc.processCount → set to 2).
      Reduce session store writes (about:configbrowser.sessionstore.interval → 300000).
      Disable hardware acceleration (SettingsGeneralUncheck Use recommended performance settings).
      Enable tab unloading (about:configbrowser.tabs.unloadOnLowMemory → true).

      These should help lower RAM usage. Let me know if you notice an improvement!

      Reply
  5. I switched from GNOME to XFCE after reading this, and my system is using way less RAM now. One issue though that after enabling ZRAM, I noticed my swap usage increased slightly.

    Is this normal?

    Reply
    • Yes, it’s normal to see a slight increase in swap usage after enabling ZRAM. Unlike traditional swap, ZRAM compresses data in RAM before swapping, making it more efficient.

      If swap usage seems too high, you can tweak the compression algorithm or adjust vm.swappiness further (e.g., setting it to 5 or even 1) with:

      sudo sysctl -w vm.swappiness=5
      
      Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.