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?
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 overloadsmmap
,sbrk
and other memory-allocating system calls (assuming you don't run any static binaries).ptrace
the processes do watch memory allocation and forking./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.
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 withstrace
but I can't think in a simple way to do it.I recommend using https://github.com/gsauthof/cgmemtime.
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.