10

How can I find the total memory usage of all processes (including child processes) with PPID of 1. For a program like nginx/httpd which forks several child processes, ps/top gives the memory usage of each process separately. I want to know the total memory usage of nginx/apache instead of the individual child processes.

2 Answers2

15

If you want something interactive, try :

atop - launch it and press 'm' (memory usage) and 'p' (group by process name).

If you want something scriptable, there's something a bit like the below - although it's not doing quite what you've asked for (PPID 1), but you can group based on e.g. process name using awk -

ps -eo pmem,vsize,cmd | grep -v '\[' | awk 'NR>2{mem[$3]+=$2}END {for(k in mem) print k " " mem[k]/1024000};' | sort -gk 2 | tail -n 10

There's probably some clever way to filter on ppid with the ps... but I'm too stupid to figure it out quickly.

1

On a distro using systemd, systemd-cgls should show memory usage per service. Doesn't always obtain the information though.

grawity
  • 501,077