Print RAM Usage of process including children

2 points by sean_pedersen ↗ HN
Prints resident set size (RAM) usage of processes including $1 in their name (includes children):

function ram() {pgrep -f $1 | xargs -I{} pmap -x {} | grep total | python -c "import sys; print('%.2f' % (sum([float([c for c in line.strip().split(' ') if c][3]) for line in list(sys.stdin)])/1024), 'MB RAM')"}

Been looking for a way to see total RAM usage of processes including their children, after a bit of googling I came up with this ugly one liner, but hey it works!

6 comments

[ 4.1 ms ] story [ 26.2 ms ] thread
(comment deleted)
(comment deleted)
Got working after I figured out the argument should be the command I’m interested in. And it must be run as root if not my process because of pmap -x

One-liners are cool.

Yup usage is simple:

$ ram firefox

Will print total firefox RAM usage.

On my machine (Arch Linux) it works without root...

(comment deleted)
All bash version of it... pgrep -f $1 | xargs -I{} pmap -x {} | grep total | awk '{sum=sum+$4}END{printf "%.2f%s", sum/1024," MB\n"}'

e.g.

$ pgrep -f bash | xargs -I{} pmap -x {} | grep total | awk '{sum=sum+$4}END{printf "%.2f%s", sum/1024," MB\n"}'

74.79 MB