This question already has an answer here:

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.

marked as duplicate by Gilles, jasonwryan, slm, Anthon, manatwork Aug 6 '13 at 7:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


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.


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:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.