3

I develop a C code on Debian. I am looking for a program to monitor memory consumption of my code in real-time. The only value I care is the maximum RAM consumption, it should hold the value even if the the program is killed (e.g. by SIGKILL) . Could anyone suggest a handy tool for it?

Normally I use top -p pidnumber, but it outputs with too much delay and if my program gets killed, it does not show the RAM consumption just before it is ended.

Angs
  • 502
  • 2
  • 7
  • 21

2 Answers2

5

You can use the Valrind tool Massif.

valgrind --tool=massif prog

On default it creates a graph of the used heap memory, but it can also show the used stack memory with

valgrind --tool=massif --stacks=yes prog

Valgrind has the tendency to slow down the execution of your program, so be warned if your application runs a long time.

For more information about Massif I can recommend the Valgrind documentation which is quite good.

2

You can change the delay in top with top -d delay or by pressing s in top.

To get the resident size every second:

while rss=$(ps -o rss= -p "${pid?}") && printf '\r%d' "$rss"; do
  sleep 1; done; echo

Or to get fancier:

sp="/-\|"
while rss=$(ps -o rss=,args= -p "${pid?}") && printf '\r[%c]%s' "$sp" "$rss"; do
  sleep 0.5; sp=${sp#?}${sp%???}; done; echo