But this is not a permanent change. The next time you run the top command, it will be sorted on CPU usage again.
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.
How can I display the top results in my terminal in real time so that the list is sorted by memory usage?
On Linux, > and < move the sort column right and left. Since the %MEM column is just right of the %CPU column, which is also the default sort column, it takes only one keystroke to switch between the two. I know, your question has the macintosh tag, that's why I'm writing this answer as a comment.
If using htop , in addition to shift + M , you will likely want to turn off the display of threads and just show the main process memory consumption with shift + H . See unix.stackexchange.com/a/10403/27902.
Or specify the sort order on the command line
# on OS-X top -o MEM # other distros top -o %MEM
@GabrielHautclocq It must depend upon your distribution of Linux, and the package bundled with it. Debian 7 uses procps-ng and there is no -o option at all in that version. SHIFT-M works for me once top is launched.
The command line option -o (o standing for «Override-sort-field») also works on my Xubuntu machine and according to the Mac man page of top it should work on a Macintosh too. If I want to short by memory usage I usually use
which sorts by the column %MEM . But I can use VIRT , RES or SHR too. On a Macintosh I would probably use mem or vsize .
I don’t know why or how but this is pretty much different between Unix systems and even between Linux distributions. For example -o isn’t even available on my Raspberry running Wheezy. It may be worth give it a try though.
Top is a Linux process and resource usage monitoring utility. It allows users to view real-time information about the running processes and threads managed by the system’s kernel. Because of its interactivity, top enables users to perform tasks, such as filtering for specific processes, filter processes by users, PID, and kill processes.
This guide will walk you through the basics of using the ps command to locate specific information about the system.
To launch the top utility, use the top command in the terminal. Using this command will spawn an interactive session showing system resource usage and running processes:
The upper part shows the resource usage. This output is similar to that of uptime and the free command in Linux.
To turn these values off, press “m” to hide the memory usage information and “l” to hide the uptime information.
To scroll through the running processes, use the up and down arrow key. To quit, press “Q”.
The lower part of top command contains information about running processes. Let us focus on this.
The output uses a column-based organization, with an identifier on each:
You can also filter processes by memory usage in top. To do this, press SHIFT + m as shown:
Top will filter the processes by memory usage in descending order. Doing this can help identify the process using the most memory, giving you a chance to take action.
To filter by actual memory usage, use the command:
Similarly, the command will filter the memory usage in descending order.
You can also interactively choose the filter parameter. To do this, press SHIFT + F and select MEM as:
To show processes from a specific user, use the command:
For example, to show processes from the ubuntu user; enter the command:
To show the parent and child processes while top is running, press V. This will give you an output similar to the one shown below:
To kill a process in top, press k and enter the PID of the process.
Press enter to execute the kill command. This will terminate the process with the specified PID.
Top is a handy utility that makes it possible to understand and manage the Linux System processes. Besides what we have discussed in this tutorial, top has tons of other functionalities.
To understand how you can customize and use top to its full potential, consider the manuals.
My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list