In Linux, the top is the utility to monitor real-time system activities such as Kernel tasks or processor activities. It is used by system administrators for managing computer tasks. The memory usage options of the top command are quite helpful for the users to decide what processes or applications their system can acquire.
This article will demonstrate how the user can display the real-time “top” command results by memory usage.
How to Sort the Memory Usage Through top Command?
This section presents various use cases of the top command to sort the memory usage of the system:
Example 1: Default Order of Memory
Open the terminal and run the top command in the terminal for monitoring the system activities:
$ top
The real-time system activities will be displayed.
Example 2: Sort the Memory Usage in Descending Order
To sort the memory usage, run the top command in the terminal and then press the “shift + M” from the keyboard:
The top command result will be sorted out by memory usage as shown in the above image.
Example 3: Memory Usage in Descending Order
Memory usage can be sorted on the first display of the top command (while the above examples show the sorting inside the top command output).
For this, use the command provided below:
$ top -o +%MEM
The results have been displayed in descending order.
Example 4: Memory Usage in Ascending Order
As the descending order explained in example 3, the same can be done with the ascending order as follows:
$ top -o -%MEM
The results have been displayed in ascending order.
For gaining more information on the top command, check out our latest guide on it.
The alternative of the top Command
There are various alternatives of the top command that work the same as the top command such as “atop”, and “htop”. The working of the “htop” utility is given below.
Example 1: Displaying Memory Usage Through htop
To install the “htop” command in Linux, execute the following command according to your Linux distribution:
$ sudo apt install htop #For Debian/Ubuntu
$ sudo yum install htop #For CentOS/RHEL
$ sudo dnf install htop #For Fedora
The “htop” utility has been installed.
Run the “htop” command in the terminal for viewing the memory usage details:
$ htop
The various system activities have been listed and you can check memory usage as well.
Conclusion
To display the “top” command result sorted by memory usage, run the “top” in the terminal and press the “Shift + M” from the keyboard to sort it. The users can also use the command “top -o +%MEM” and “top -o -%MEM” directly in the terminal to get the descending or ascending order of memory usage. This write-up has illustrated methods to display the top results sorted by memory usage. Additionally, the alternative of the top utility has also been illustrated in this post.