Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
How do you want to account for shared libraries that are simultaneously used by your process tree (perhaps multiple times) and by unrelated processes? –  mpez0 Sep 9 '10 at 19:34

3 Answers 3

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.

share|improve this answer

pmap shows all memory allocated to a process, and even gives you the total. Detecting the child processes is harder, you can maybe combine it with strace but I can't think in a simple way to do it.

share|improve this answer

The ps command can be used to measure the amount of memory that each process uses.

%MEM shows the percentage of physical memory that a process uses. While not always the full picture it can identify processes that are responsible for system paging and swapping.

SZ shows the approximate virtual size of a process.

RSS is the Resident Set size and is the actual amount of memory that the process was using when ps was run.

Check your man ps page for options for listing process trees.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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