Human readable memory usage in Linux per process

LinuxI like to tune my VPS to use as little memory as possible. Not because I really want to; but because I just don’t have the money to spend on a 2+ GB RAM VPS and I would like to run Jira.

In order to do this I keep a close eye on the processes running and how much memory each takes.

For this I found (and tweaked) the following bash command (originally found here):

ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }'

This command will output every process and its memory usage in human readable (thus megabytes) format. For your convenience it is sorted by memory size descending (from highest to lowest).

An example of its use:

[email protected]:~$ ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }'
0.00 Mb COMMAND
116.18 Mb /usr/sbin/mysqld
11.58 Mb /usr/sbin/apache2 -k start
11.58 Mb /usr/sbin/apache2 -k start
11.58 Mb /usr/sbin/apache2 -k start

(I am sure I can tune the above to use less memory; this is merely an example)

I hope it will be as useful to you as it is to me,

Until next time!

7 thoughts on “Human readable memory usage in Linux per process

  1. Thank you for noticing! It appears that the Geshi plugin which I use does an extra encoding. I have fixed the issue and it should show up all right now.

  2. It would be good to see a way to grep out a particular item and add up the memory usage, for instance for one particular PHP FPM pool.

Leave a Reply

Your email address will not be published. Required fields are marked *