How to Sort Top Command Based on Memory Usage
Top command shows the most CPU consuming processes first. Learn how to sort top on memory usage instead.
As a Linux user, you cannot avoid the top command. This simple command gives an overview of the all running system process. It refreshes the stats every three seconds and gives you the feeling of continuously monitoring the processes.
By default, the output of the top command is sorted on the CPU consumption. This means that you see the processes that consume the most CPU is on the top of the command.
But what if you want to see the processes that consume the most of the RAM? You can sort top command based on memory usage instead of CPU consumption.
It's quite simple. Here's how to do that.
Sort top command to show memory usage by processes
Step 1: Run the top command as usual.
Step 2: Press Shift+M to sort the top command output by memory. That's it. It will instantly start showing processes with most memory usage.
But this is not a permanent change. The next time you run the top command, it will be sorted on CPU usage again.
Sort the top command on memory, virtual memory or any other parameter
The Shift+M keyboard shortcut is handy for checking memory usage per process. However, if you want to sort top command on some other field like virtual memory, time spent, shared memory etc, you can use the interactive mode of the top command.
Step 1: Run the top command, of course.
Step 2: Press Shift+F to enter the interactive mode.
Step 3: Use the arrow key to choose a different parameter like %MEM, TIME, VIRT etc. When you are at the desired parameter, press S to sort on it.
You can see the current sort field in the top right corner. Press Esc or Q to quit the interactive mode. Now the top command output will be sorted on your selected field.
That's handy, right? Top command is very powerful bit unfortunately, many people do not know how to use it properly and effectively. If you want more details, I highly recommend our article on using top command.
Any questions or suggestions? Please leave a comment.
Creator of Linux Handbook and It's FOSS. An ardent Linux user & open source promoter. Huge fan of classic detective mysteries from Agatha Christie and Sherlock Holmes to Columbo & Ellery Queen.