Determining maximum memory usage for an extremely short process

memoryprocess

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.

Best Answer

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== 
==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== 
==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== 
==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.

Related Question