sodcalculator

10 Tech How-To

Highest CPU Memory Usage on Linux: Top Processes

Guide 10-28976 · Updated · Step by step

The processes that use the highest CPU and memory on a Linux system are typically identified using the top or htop commands, which display live resource consumption. The exact process depends on your workload, but common culprits include database servers, web servers, compilation jobs, and runaway user applications. This article explains how to find them, interpret the output, and manage resource-heavy processes safely.

Understanding CPU and Memory Usage in Linux

Linux tracks resource usage per process. CPU usage is the percentage of a single core (or multiple cores) a process is using, while memory usage is the amount of physical RAM (resident set size, RSS) a process occupies. The top command shows both, sorted by CPU by default. Memory is also shown as a percentage of total RAM. A process can be CPU-intensive (e.g., video encoding) or memory-intensive (e.g., a large database cache), and often both occur together.

Using top and htop to Find the Highest Consumers

The most direct way is to run top in a terminal. It refreshes every few seconds and lists processes in descending order of CPU usage. To sort by memory, press M (capital M) while in top. The htop command is a more user-friendly alternative with color-coded bars and easier sorting (use the F6 key to sort by a column). Both tools show the PID (process ID), user, CPU%, MEM%, and the command name.

Interpreting the Output: What Do the Numbers Mean?

In top, the %CPU column can exceed 100% on multi-core systems because it represents the total CPU time across all cores. For example, a process using 200% means it is using two full cores. The %MEM column is the process’s RSS divided by total RAM. The VIRT column shows virtual memory, which includes shared libraries and allocated but unused memory, so it is not a reliable indicator of actual RAM pressure. Focus on RES (resident) for real memory usage.

Common High-Usage Processes and Why They Happen

  • Database servers (e.g., MySQL, PostgreSQL) often use large amounts of memory for caching and can spike CPU during complex queries.
  • Web servers (e.g., Apache, Nginx) under heavy traffic consume CPU and memory per connection.
  • Compilation tools (gcc, make) are CPU-intensive during software builds.
  • Video encoding/rendering (ffmpeg, Blender) uses high CPU for extended periods.
  • Runaway processes due to bugs or infinite loops can consume 100% CPU indefinitely.

How to Manage or Kill Resource-Heavy Processes

Once you identify a process, you can take action. Use kill PID to terminate it gracefully, or kill -9 PID to force kill if it does not respond. For a specific user, you can use pkill -u username to kill all their processes. To reduce priority, use renice to lower CPU priority (e.g., renice 10 PID). Always be careful: killing system processes can crash your system. For a temporary fix, you can pause a process with kill -STOP PID and resume with kill -CONT PID.

Using ps and Other Commands for Detailed Analysis

The ps command can provide a snapshot without continuous updates. To see top CPU consumers, run ps aux --sort=-%cpu | head. For memory, use ps aux --sort=-%mem | head. The pidstat command (from sysstat) gives per-process CPU and memory statistics over time. For a historical view, atop logs resource usage to disk. These tools help you identify patterns, such as a memory leak that grows over time.

Memory Leaks and Zombie Processes

A memory leak occurs when a process allocates memory but never frees it, causing its RSS to grow indefinitely. You can spot this by monitoring the same process over time with top. Zombie processes are defunct processes that have finished but still appear in the process table because the parent hasn’t reaped them. They consume no CPU or memory but can clutter the list. They are usually harmless and disappear when the parent process exits.

Frequently Asked Questions

How do I find the process using the most memory?

Run top and press M to sort by memory usage. Alternatively, use ps aux --sort=-%mem | head to see the top memory consumers in a single snapshot.

What is the difference between %CPU and %MEM in top?

%CPU is the percentage of a single CPU core (or multiple cores) the process is using. %MEM is the percentage of total physical RAM the process is using. Both are relative to your system’s total resources.

Can a process use more than 100% CPU?

Yes, on multi-core systems, a process can use multiple cores simultaneously, so %CPU can exceed 100%. For example, 200% means two full cores are being used.

Why does a process show high VIRT but low RES memory?

VIRT includes shared libraries and memory that is allocated but not yet touched. RES is the actual physical RAM currently used. High VIRT is normal for many applications and does not indicate memory pressure.

Is it safe to kill a high-CPU process?

It depends on what the process is. Killing a user application is usually safe, but killing system processes (like systemd or kernel threads) can cause instability. Always check the process name and user before killing.

How can I prevent a process from using too much CPU?

Use the nice command to start a process with lower priority (e.g., nice -n 10 command). For an already running process, use renice. You can also use control groups (cgroups) to limit CPU and memory usage for specific processes.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →