top is a powerful command for periodically displaying a sorted list of system processes. On Linux its default sort key is %CPU—but when you’re chasing a memory leak or a RAM-hungry process, you’ll want to sort by memory instead. Here are three ways to do it, plus how to read the memory columns.
Table of Contents
Understanding the top command
top displays information about the currently running processes on your system: CPU usage, memory usage, and swap usage per process, along with each process’s PID, owner, and command line. It’s a go-to tool for understanding what’s running and how it’s using resources.
Quick answer: 3 ways to sort top by memory
- Press
Shift+Mafter runningtop. - Use the interactive field menu (
Shift+F) to pick%MEM. - Launch with
top -o +%MEM.
Method 1: Press Shift+M (the fastest way)
Run top, then press Shift+M (i.e., M). The process list immediately re-sorts by memory usage, highest first:
top
After pressing M, the output looks like this:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2413 mysql 20 0 2588104 512340 18220 S 1.3 12.7 5:42.10 mysqld
1876 www-data 20 0 742160 210480 22140 S 0.7 5.2 0:58.44 php-fpm
3120 elastic 20 0 3814500 190244 12040 S 0.3 4.7 2:11.90 java
987 root 20 0 412300 88120 9440 S 0.0 2.2 0:12.03 dockerd
Tip: a leading + forces high-to-low sorting, while a leading - gives low-to-high. top sorts descending by default.
Method 2: Choose the sort field interactively (Shift+F)
For finer control, use the field-management screen:
- Press
Shift+Fto enter the field menu. - Use the up/down arrows until
%MEMis highlighted. - Press
sto select%MEMas the sort field. - Press
Enterto save your selection. - Press
qto exit the menu and return to the live view.
The list is now sorted by %MEM. This method also lets you add, remove, or reorder columns while you’re in the menu.
Method 3: Sort from the command line (top -o +%MEM)
You can set the sort field up front so top starts sorted by memory:
top -o +%MEM
This is handy for aliases or scripts where you always want the memory view.
Sort by memory in batch mode
Batch mode (-b) makes top machine-readable so you can log the top memory consumers over time. This snippet captures the highest-memory processes every 5 seconds, ten times:
See also: Mastering the Linux Command Line — Your Complete Free Training Guide
for i in {1..10}; do date; top -b -o +%MEM -n 1 | head -n 17 | tail -n 11; sleep 5; done
Sample output for one iteration:
Sat Aug 30 09:41:12 UTC 2026
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2413 mysql 20 0 2588104 512340 18220 S 1.3 12.7 5:42.10 mysqld
1876 www-data 20 0 742160 210480 22140 S 0.7 5.2 0:58.44 php-fpm
3120 elastic 20 0 3814500 190244 12040 S 0.3 4.7 2:11.90 java
-bruns in batch mode (plain text, no screen control).-o +%MEMsorts by memory.-n 1takes a single iteration and exits—important in batch mode, otherwisetopkeeps producing iterations indefinitely.head -n 17 | tail -n 11trims the output to the column header plus the top 10 processes.
Reading the memory columns: %MEM, VIRT, and RES
top exposes three memory-related metrics:
%MEM— the percentage of the system’s physical memory in use by the process.VIRT— virtual memory size: the total memory mapped by the process.RES— resident set size: the physical memory the process is actually using.
How they map to ps and what they really mean:
VIRT(calledVSZinps) is the total amount of memory mapped by a process—the sum of all regions listed in/proc/<pid>/maps. Much of it may never be resident in RAM, so a largeVIRTalone is rarely a concern.RES(calledRSSinps) is the portion mapped to physical pages of memory. This is much closer to the process’s real memory footprint.
Note:
RES/RSScounts shared pages (like shared libraries) against every process using them, so summingRESacross processes overstates total usage. For an honest per-process figure, the proportional set size (PSS, viasmem) is more accurate.
Sort by VIRT or RES directly
The same -o option works for the raw memory metrics:
# Sort by virtual memory size
for i in {1..10}; do date; top -b -o +VIRT -n 1 | head -n 17 | tail -n 11; sleep 5; done
# Sort by resident set size (real physical memory)
for i in {1..10}; do date; top -b -o +RES -n 1 | head -n 17 | tail -n 11; sleep 5; done
For diagnosing real memory pressure, sorting by RES (or %MEM) is usually the most meaningful.
Summary
| Goal | Method |
|---|---|
| Quick memory sort | Run top, press Shift+M |
| Pick the sort field | top, Shift+F, select %MEM, s, Enter |
| Start sorted by memory | top -o +%MEM |
| Log top memory users | top -b -o +%MEM -n 1 in a loop |
| Sort by raw metric | top -o +VIRT or top -o +RES |
Press Shift+M for a quick interactive look, use top -o +%MEM when scripting, and remember that RES reflects real physical memory while VIRT is mostly address space that may never be resident.


