Get maximum memory usage of linux executable

memoryscriptingshelltime

I need a shell script which will retrieve the maximum memory consumption of a linux executable. The executable may spawn child processes using significant amounts of RAM which should be included in the total.

I've tried /usr/bin/time -f "%M" /path/to/executable, but this always yields 0 though using ps I can verify the process is indeed consuming significant RAM.

Why is time giving me 0 all the time, and how can I get the number I'm looking for?

Best Answer

I think time -f %M only works in recent Linux kernels (experimentally, it's not supported in 2.6.26/amd64, and it is supported in 2.6.32/i386).

An earlier thread at Stack Overflow didn't turn up much.

Without kernel support, monitoring memory usage is fairly hard. There are a few ways to do it:

  • LD_PRELOAD a small library that overloads mmap, sbrk and other memory-allocating system calls (assuming you don't run any static binaries).
  • ptrace the processes do watch memory allocation and forking.
  • Watch /proc/ (works for a single process only, and you don't know what happens between measures).

These ways all require some programming; I don't know of an existing tool.