Skip to content

How to Find Top CPU and Memory Consuming Processes in Linux

Find the processes using the most CPU and memory in Linux with ps, top, htop, pidstat and systemd-cgtop, why ps %CPU is misleading, plus a quick health-report script.

Steps
6
Category
Linux
On this page 6 steps

When a Linux server feels slow, two resources are usually behind it: CPU and memory. A process stuck in a loop can keep a CPU core at 100%, and a process that keeps growing can push the system into swap until everything crawls. This guide shows how to find the top CPU- and memory-consuming processes in one pass with ps, top, htop and pidstat, and how to avoid the most common mistake: trusting the %CPU number from ps.

Find top memory and CPU consuming processes in Linux
Find the top memory- and CPU-consuming processes in Linux

Quick Answer

Top 10 processes by memory:

ps -eo pid,user,%cpu,%mem,comm --sort=-%mem | head -n 11

Top 10 processes by CPU usage right now (measured over 5 seconds):

top -b -n 2 -d 5 -o %CPU | awk '/^top -/{n++} n==2' | sed -n '7,17p'

The rest of this article explains why the CPU command is different and which tool to use when.

Step 1 of 6Check Whether CPU or Memory Is the Problem

Before looking at single processes, check the whole system. Two commands tell you which resource is short:

uptime
free -h
 02:17:41 up 38 days,  8:17,  3 users,  load average: 1.08, 0.78, 0.54

               total        used        free      shared  buff/cache   available
Mem:           7.8Gi       5.0Gi       1.6Gi       212Mi       1.6Gi       2.7Gi
Swap:          8.0Gi       1.9Gi       6.1Gi
  • Load average shows the average number of processes running or waiting over the last 1, 5 and 15 minutes. Compare it with the number of CPU cores (nproc). A load well above the core count for a while means the CPU, or sometimes the disk, is the bottleneck.
  • available in free -h is the memory that can be given to programs without swapping. If it’s low and swap use keeps rising, memory is the problem. Don’t worry about a low free value: Linux uses spare RAM as cache and gives it back when needed.

Step 2 of 6Find the Top Processes with ps

ps prints a one-time list, which is ideal for scripts and quick SSH checks. Pick the columns with -o and the sort order with --sort; a minus sign sorts largest first.

By memory

ps -eo pid,user,%cpu,%mem,rss,comm --sort=-%mem | head -n 11

%MEM is the share of physical RAM the process uses and RSS is the same amount in kilobytes. head -n 11 keeps the header plus 10 processes. Use cmd instead of comm to see full command lines with arguments.

ps command listing the top 10 processes by memory usage
Top 10 processes by memory usage

For a deeper look at memory, including totals per program, shared memory and swap per process, see how to list processes by memory usage in Linux.

By CPU

ps -eo pid,user,%cpu,%mem,comm --sort=-%cpu | head -n 11
ps command listing the top 10 processes by CPU usage
Top 10 processes by CPU usage

Important: the %CPU column in ps is not current usage. It’s the CPU time the process has used divided by how long it has been running, an average over its whole life. A web server that has been idle for a week but was busy at startup shows a low number even while it’s spiking now, and a short-lived process can show a high number for a fraction of a second of work. To see what is using the CPU now, use top or pidstat below.

Filter by a threshold

To list only processes above 5% CPU or 5% memory, use awk on the numeric columns. Keep the command name as the last column, because commands can contain spaces that shift the column numbers:

ps -eo pid,user,%cpu,%mem,comm --sort=-%mem --no-headers | awk '$3 > 5.0 || $4 > 5.0'
    846 root      1.1 19.8 dockerd
3444189 mysql     3.2  9.7 mysqld
3489172 ubuntu    0.1  5.1 next-server

Here $3 is %CPU and $4 is %MEM. Change || to && to show only processes over both limits.

Step 3 of 6Watch Live with top

top measures CPU usage between refreshes, so its %CPU shows what each process is doing now. Start it and use these keys:

top
KeyAction
Shift + PSort by CPU (the default)
Shift + MSort by memory
1Show each CPU core separately
cShow the full command line
eChange memory units in the process list (KiB, MiB, GiB)
kSend a signal to a process by PID
qQuit

On a multi-core server, one process can show more than 100% CPU: 200% means it is using two full cores. Press Shift + I to switch to “Irix mode off”, where the percentage is divided by the number of cores.

Save a top snapshot

For a list you can save or paste into a ticket, run top in batch mode. Its first update has no previous sample to compare with, so take two updates and keep the second:

top -b -n 2 -d 5 -o %CPU | awk '/^top -/{n++} n==2' | sed -n '7,17p'
    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 316651 www-data  20   0  338480 118784  83712 S   9.0   1.5   0:05.22 php-fpm+
 317648 www-data  20   0  335724 111636  79360 S   9.0   1.4   0:00.61 php-fpm+
3444189 mysql     20   0 1949276 789528  19840 S   2.5   9.7      8,24 mysqld
3444010 redis     20   0 2483016 278904   3200 S   1.5   3.4 125:56.07 redis-s+

-d 5 sets 5 seconds between the two updates, and sed -n '7,17p' prints the column header and 10 processes. Replace -o %CPU with -o %MEM to sort by memory. Our top command guide explains every column in the output.

Step 4 of 6Use htop for an Easier View

htop shows the same data with colour bars per core, mouse support and scrolling. Install it with sudo apt install htop on Ubuntu and Debian, or sudo dnf install htop on RHEL, Rocky Linux, AlmaLinux and Fedora.

Press Shift + P to sort by CPU, Shift + M to sort by memory, F5 for a tree view that groups workers under their parent, and F9 to send a signal to the selected process. See our htop command guide for all shortcuts.

Step 5 of 6Measure Over Time with pidstat

A single snapshot can miss short spikes. pidstat, from the sysstat package, samples every process over an interval and prints an average:

sudo apt install sysstat

To find the processes that used the most CPU over 5 seconds:

pidstat -u 5 1 | awk '/^Average/ && $8+0 > 0' | sort -k8 -nr | head -n 10
Average:       33    316764    3.39    0.40    0.00    0.00    3.79     -  php-fpm8.4
Average:       33    317648    2.40    0.00    0.00    0.00    2.40     -  php-fpm8.4
Average:      109   3444189    1.80    0.40    0.00    0.00    2.20     -  mysqld

The columns are UID, PID, user-space CPU (%usr), kernel CPU (%system), guest, %wait, total %CPU, CPU core and command. A high %wait means the process is ready to run but waiting for a CPU: a sign the server needs more cores or less work. For memory, pidstat -r 5 1 shows RSS, %MEM and page faults for each process. Use a longer interval, such as pidstat -u 60 1, to catch processes that spike every few seconds.

Step 6 of 6Check by Service, Not Just by Process

Web servers, PHP-FPM and databases split work across many processes, so no single one may look large. systemd-cgtop adds up CPU and memory for each systemd service:

systemd-cgtop

It sorts by CPU by default. Press m to sort by memory and q to quit. For Docker containers, docker stats shows CPU and memory per container.

A Script for a Quick Health Report

This script prints the load, memory, and the top 5 processes by current CPU and by memory in one go. Save it as /usr/local/bin/top-procs:

#!/bin/bash
# Quick CPU and memory report: load, RAM, top 5 processes each.
echo "== Load ($(nproc) cores): $(cut -d' ' -f1-3 /proc/loadavg)"
echo "== Memory"
free -h
echo
echo "== Top 5 by CPU (now, 3-second sample)"
top -b -n 2 -d 3 -o %CPU | awk '/^top -/{n++} n==2' | sed -n '7,12p'
echo
echo "== Top 5 by memory"
ps -eo pid,user,%cpu,%mem,rss,comm --sort=-%mem | head -n 6

Make it executable and run it:

sudo chmod +x /usr/local/bin/top-procs
top-procs

To keep a history for later troubleshooting, run it from root’s crontab (sudo crontab -e) every 5 minutes:

*/5 * * * * (date; /usr/local/bin/top-procs) >> /var/log/top-procs.log 2>&1

Each run adds about 2 KB to the log, so delete it or set up log rotation once you’ve found the cause. For permanent monitoring, use a tool such as Prometheus with node_exporter, Netdata or Zabbix.

What to Do Next

Once you know which process is responsible:

  • Restart a misbehaving service with sudo systemctl restart <service>. For a stray process, send a normal kill <PID> first and use kill -9 only if it doesn’t stop. See how to kill a process by ID.
  • Lower the CPU priority of a heavy batch job with renice 10 -p <PID>, so interactive work gets the CPU first. This only affects CPU: it doesn’t reduce memory use.
  • Limit a service with systemd: sudo systemctl edit <service>, then add CPUQuota=50% or MemoryMax=1G under [Service]. For containers, see CPU and memory limits in Docker.
  • Tune the application, for example PHP-FPM’s worker count (reduce PHP-FPM memory usage) or a database’s buffer sizes.
  • Check for runaway CPU in detail with our guide to the top CPU-consuming processes in Linux.

Quick Reference

TaskCommand
Top 10 by memoryps -eo pid,user,%cpu,%mem,comm --sort=-%mem | head -n 11
Top 10 by lifetime-average CPUps -eo pid,user,%cpu,%mem,comm --sort=-%cpu | head -n 11
Top 10 by current CPUtop -b -n 2 -d 5 -o %CPU | awk '/^top -/{n++} n==2' | sed -n '7,17p'
Live viewtop or htop, then Shift + P / Shift + M
CPU averaged over timepidstat -u 5 1
Per servicesystemd-cgtop
Load and memory overviewuptime and free -h

Frequently Asked Questions

How do I find which process is using the most CPU in Linux?

Run top. It sorts by CPU usage by default, so the busiest process is at the top. For a saved snapshot, use top -b -n 2 -d 5 -o %CPU and keep the second update, or pidstat -u 5 1 for a 5-second average.

How do I find which process is using the most memory?

Run ps -eo pid,user,%mem,rss,comm --sort=-%mem | head -n 11, or press Shift + M in top or htop.

Why does ps show different CPU usage than top?

ps shows the average CPU usage over the process’s whole lifetime. top shows usage since its last refresh, which is what the process is doing now. Use top or pidstat to find a current CPU spike.

Why does a process show more than 100% CPU?

top counts 100% per core, so a process using two cores shows 200%. Press Shift + I in top to divide by the number of cores.

Is high memory usage always a problem?

No. Databases cache data in RAM on purpose, and Linux uses free memory as file cache. It’s a problem when the available value in free -h is low, swap use keeps growing, or the kernel’s OOM killer starts stopping processes.

Conclusion

To find the top CPU- and memory-consuming processes in Linux, start with uptime and free -h to see which resource is short. Use ps --sort=-%mem for memory, and top, htop or pidstat for CPU, because the %CPU in ps is a lifetime average, not current use. For services with many workers, check systemd-cgtop, and keep the top-procs script in cron if problems happen when you’re not watching.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top