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.
Table of Contents
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
- press shift+m after running the top command
- sort mem usage per process in the interactive menu. More details are below.
- run command top -o +%MEM
Get Your Free Linux training!
Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!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:
- Troubleshooting Disk Usage In Linux
- Troubleshooting High Load Average on Linux
- Troubleshoot Network Slow Problems In Linux
- Troubleshoot high iowait issue on Linux
Linux Learning Guide: