Linux is the top choice of an operating system when it comes to server-based systems. System administrators and even advanced users of Linux have a good deal of knowledge of the command line and know specific commands or specific arguments to certain commands, to get the job in hand done.
Not only for servers but also for regular desktop usage, it is quite handy to know the processes and programs which are consuming the most memory. This gives an idea of the system resource usage and the user can then take appropriate action to free up the memory.
We will use the top command-line utility, which is a task manager in Unix and Linux systems that shows all the details about running processes. When executed without any argument, top shows all the Linux processes by their CPU usage, shared and private memory usage, etc. in percentage.
Note that it shows the output in an interactive format and not simply print the output. The column
'%MEM' is showing the memory usage of each program.
Find Processes Using Highest Memory Usage
To show the most memory consuming processes, we make use of the
'-o' flag at the top to sort the output.
$ top -o %MEM
The output of the top command is real-time; i.e. if there is a change in memory or resource usage for any program, the output will immediately show the changed values in the interactive output.
Find Top 10 Processes Using Highest Memory Usage
To get the top 5 or top 10 largest memory consuming processes from this output, we can simply pipe the output to command ‘head‘, which will display only the first ‘n’ rows of any output.
$ top -o %MEM | head -n 15
Note that the summary of resource usage is displayed at the beginning; hence 7 rows of those are considered by ‘head‘ and we can see 8 processes in the output. The user should thus enter the number as ‘required processes to see’ plus 7.
For example, If the top 15 processes are to be seen, he should pass the argument as 22.
In this article, we have seen how to find the top running processes by memory usage in Linux, using the top command-line utility. Make sure you check out the manual page of the top using ‘man top‘ to learn more about the command.
If you have any questions or feedback, let us know in the comments below!