Linux top sort by memory can help us find out which processes use more memory.
In linux/unix, there are two ways to sort by memory using the top command:
- top option -o
- top interactive mode
Let’s take a look at how to use the top command to sort processes by memory usage.
top option -o
The linux top command can use the -o option to sort the keys that support sorting fields in ascending or descending order.
Memory supports sorting related keys:
- mem Physical memory footprint of the process.
- rprvt Resident private address space size.
- purg Purgeable memory size.
- vsize Total memory size.
- vprvt Private address space size.
- kprvt Private kernel memory size.
- kshrd Shared kernel memory size.
Top sort syntax:
➜ ~ top -o key
Top example 1
In the following top example, we will use the top command to sort the physical memory usage of the process in descending order.
➜ ~ top -o mem
# OR
➜ ~ top -o -mem
![linux sort by memory](https://www.linuxcommands.site/wp-content/uploads/2020/06/image-2-1024x329.png)
When using the top -o option to sort key, you can use the symbol before the key: ” + “ or ” – “ :
- + Indicates descending order;
- – Indicates ascending order;
top -o defaults to ascending order.
Top example 2
top Sort by ascending memory usage:
➜ ~ top -o +mem
The top -o option can not only sort memory, but also sort other supported sort fields (support sort key, see man top).
Use the top -o option to sort the CPU usage as follows:
➜ ~ top -o cpu
top interactive mode
In the top interactive mode, you need to execute the top command directly on the terminal, and then enter the interactive command on the top monitoring page.
Interactive mode, top sort command:
okey Order the process display by sorting on key in descending order.
Examples
1. Open the terminal and execute the top command.
➜ ~ top
2. Enter the top interactive sorting command: omem
Note the red box in the picture above
a. When you enter the letter ” o “, you will find that the red box shows the default sort order of the current top command: cpu descending order
primary key [-cpu]:
b. Continue typing ” mem “ and press Enter. You will find that the top monitoring page has been sorted in descending order of memory usage.
c. When you enter the letter ” o “ again, the monitoring page will display the sorting method used by the current top command.
primary key [-mem]:
-mem: Sort by memory usage in descending order
This interactive mode is also sorted by other keys.
ok.
Linux top two ways to sort by memory: top -o option and top interactive mode, have you learned?