Categories
Coding

Memory Usage Statistics via Bash

I was just experimenting with a couple of useful commands (one is basically from the ps man page), and I want to record them here before I forget them.

Firstly,

ps -o pid,pmem,user --sort pmem --user [myuser]

Will show the currently running processes for [myuser], and for each process, the percentage of RSS (resident set size) of that process with respect to the total amount of physical memory on the machine. Very useful. If you want to see the entire process virtual size, add the vsize field to the field list above.

Secondly:

echo `cat /proc/meminfo | /bin/awk '/MemTotal/ {tot=$2} /MemFree/ {print $2/tot}'`

I’m sure there is a much nicer way to do this in awk, but I am an awk newbie. This returns the amount of free memory expressed as a percentage of total physical memory.

Awk doesn’t seem to have a round() function, so a version with more readable output would be:

echo `cat /proc/meminfo | /bin/awk '/MemTotal/ {tot=$2} /MemFree/ {print int($2/tot*10
0) "% memory free"}'`