How to Sort the top Command by Memory Usage in Linux (3 Ways)

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.

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

  1. Press Shift+M after running top.
  2. Use the interactive field menu (Shift+F) to pick %MEM.
  3. 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:

  1. Press Shift+F to enter the field menu.
  2. Use the up/down arrows until %MEM is highlighted.
  3. Press s to select %MEM as the sort field.
  4. Press Enter to save your selection.
  5. Press q to 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
  • -b runs in batch mode (plain text, no screen control).
  • -o +%MEM sorts by memory.
  • -n 1 takes a single iteration and exits—important in batch mode, otherwise top keeps producing iterations indefinitely.
  • head -n 17 | tail -n 11 trims the output to the column header plus the top 10 processes.

Reading the memory columns: %MEMVIRT, 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 (called VSZ in ps) 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 large VIRT alone is rarely a concern.
  • RES (called RSS in ps) is the portion mapped to physical pages of memory. This is much closer to the process’s real memory footprint.

Note: RES/RSS counts shared pages (like shared libraries) against every process using them, so summing RES across processes overstates total usage. For an honest per-process figure, the proportional set size (PSS, via smem) 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

GoalMethod
Quick memory sortRun top, press Shift+M
Pick the sort fieldtopShift+F, select %MEMsEnter
Start sorted by memorytop -o +%MEM
Log top memory userstop -b -o +%MEM -n 1 in a loop
Sort by raw metrictop -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.

Avatar photo
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 275

Leave a Reply

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