Skip to Content

top-memory

Linux Sort By Memory in TOP Command 

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.

3 Ways to Sort by Memory in Top Command

  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

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 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/<PID>/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 Learning Guide: