howtouselinux

3 Ways to make Top Command sort by Memory

Table of Contents

Top is a very powerful command to periodically display a sorted list of system processes. The default sorting key is %CPU on Linux. Below we collect 3 ways to sort processes by memory.

Understanding top command in Linux

The top command in Linux is used to display information about the current running processes on your system. The top command can be used to see the CPU usage, memory usage, and swap usage for each process.

It can also be used to see the PID, username, and command line for each process. The top command is a useful tool for understanding what processes are running on your system and how they’re using resources.

3 Ways to Sort Top Command by memory usage

  1. press shift+m after running the top command
  2. sort mem usage per process in the interactive menu. More details are below.
  3. run command top -o +%MEM

 

The best way to sort the top command by memory usage is by pressing shift+m after running the top command. Open a terminal window. Type in top command. This will display the information about the current running processes on your system. Press shift+m. This will sort the process by memory usage.

Tip: A leading ‘+’ will force sorting high to low, whereas a ‘-” will ensure a low to high ordering.

Sort By memory Usage per-process in the top command interactive menu

  • press Shift+f to enter the interactive menu
  • press the up or down arrow until the %MEM choice is highlighted
  • press s to select %MEM choice
  • press enter to save your selection
  • press q to exit the interactive menu

 

Sort By Memory in top batch mode

We can use top batch mode to capture the process info. The following example will output the highest memory process in batch mode.

for i in {1..10};do date; top -b -o +%MEM | head -n 17|tail -11;sleep 5;done

Sort By VIRT RES Memory Metrics in top command

We have the following metrics to check memory resources in the top command.

  • %MEM: The percentage of the system’s physical memory in use by this process
  • VIRT: Virtual memory size
  • RES: Resident set size: the total physical memory in use by this process

 

Here is more info about VIRT and RES.

  • VIRT: Called VSZ in the ps command and VIRT in top, this is the total amount of memory mapped by a process. It is the sum of all the regions shown in
    /proc//map.
  • RES: Called RSS in ps and RES in top, this is the sum of memory that is mapped to physical pages of memory. This gets closer to the actual memory budget of the process.

 

So we can also use the following commands to sort these two memory metrics.

  • for i in {1..10};do date; top -b -o +VIRT | head -n 17|tail -11;sleep 5;done
  • for i in {1..10};do date; top -b -o +RES | head -n 17|tail -11;sleep 5;done

Linux Troubleshooting Guide:

Linux Learning Guide:

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.