I have a program I am testing and I need to determine the maximum memory used by the process. I know that in general ps -aux can give me this information, but the process runs in under 1/10 seconds so getting ps to catch it isn't a good option.

Are there any other good options for getting this kind of information? I am running some benchmarks on a program to see if I can improve its memory usage.

  • Run it under a debugger and set a breakpoint?
    – Celada
    Apr 11, 2015 at 20:55

3 Answers 3


valgrind will give you this information, along with a number of other statistics on memory use (and it will tell you about memory leaks etc.). It will slow the program down somewhat, but since yours is a short-lived process it shouldn't be an issue.

Here's example output from running ls:

==7051== HEAP SUMMARY:
==7051==     in use at exit: 351,689 bytes in 838 blocks
==7051==   total heap usage: 1,049 allocs, 211 frees, 688,325 bytes allocated
==7051== LEAK SUMMARY:
==7051==    definitely lost: 0 bytes in 0 blocks
==7051==    indirectly lost: 0 bytes in 0 blocks
==7051==      possibly lost: 0 bytes in 0 blocks
==7051==    still reachable: 351,689 bytes in 838 blocks
==7051==         suppressed: 0 bytes in 0 blocks
==7051== Rerun with --leak-check=full to see details of leaked memory
==7051== For counts of detected and suppressed errors, rerun with: -v
==7051== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

The specific information you're looking for is given by the "total heap usage" line.


Use GNU time (which has many more features that the builtin's "time" from bash) :

$ sudo apt-get install time
$ \time prog >/dev/null 
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 2380maxresident)k
0inputs+0outputs (0major+119minor)pagefaults 0swaps

The \time escaping explicitly asks not to use the builtin, /usr/bin/time would work too. There are many more display possibilites, use 'man time'.


This is an old thread, but I've just come across it when I also needed to check the memory requirements of a short running processes.

I've done some investigating and the accepted answer appears to be incorrect. The OP and myself are trying to find the maximum amount of memory that will be used at any point by the program, the peak memory demand.

The total heap usage metric from valgrind does not measure this, it instead measures the total of all allocations during execution. So if there were 100 iterations of a loop which allocated and then freed 1MB this would contribute 100MB to the total heap usage metric even though the peak memory demand of this is only 1MB.

Valgrind does include a tool massif which can be used to find the peak memory demand of a program, which can visualised using the massif-visualizer tool:

valgrind --tool=massif ./<your program>

massif-visualizer massif.out.<num>

This tool will plot amongst other things the total heap usage over time, and identifies the correct peak heap demand of the program. This process is described in more detail here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .