5

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?

Isaac Sutherland
  • 787
  • 2
  • 9
  • 17
  • 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 09 '10 at 19:34

4 Answers4

2

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.

1

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.

coredump
  • 12,713
  • 2
  • 36
  • 56
1

I recommend using https://github.com/gsauthof/cgmemtime.

cgmemtime measures the high-water RSS+CACHE memory usage of a process and its descendant processes.

To be able to do so it puts the process into its own cgroup.

For example process A allocates 10 MiB and forks a child B that allocates 20 MiB and that forks a child C that allocates 30 MiB. All three processes share a time window where their allocations result in corresponding RSS (resident set size) memory usage.

The question now is: How much memory is actually used as a result of running A?

Answer: 60 MiB

cgmemtime is the tool to answer such questions.

Vlad Frolov
  • 596
  • 4
  • 5
0

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.

Mike Jr
  • 187
  • 1
  • 7