i'd like to measure the total memory (like RAM) used by a program during its execution. Ideally, something like time
that runs the program and print stats when it exits.
Is there such a standard tool in the unix / linux / bsd world? Thanks.
EDIT:
the program i'm trying to profile takes fractions of a second to execute, so anything that implies "while running check this" won't work. that's why i'm asking for a "time" like tool.
The
strace
command might be of some use. It will show you in what system calls the time is being taken up:On Linux you can read /proc/[pid]/status; the VmPeak field is the maximum virtual memory size, VmHWM is the maximum resident set size.
The getrusage() syscall might, or might not, help. The struct rusage contains e.g. a maxrss field, but at least on Linux this is never filled in.
If you don't mind repeating the execution a few times you can use ulimit -Hv to set the memory limit for the shell (in Bash) and then binary search the minimum when the application successfully exits.
I'd suggest valgrind. The massif app will report heap usage stats, at least.
No standard tool except
ps
(which is not so standard in regards to options/output too). This will not give youtime
-like usability.You can use
nmon
to gather system-wide statistics, it captures memory usage of processes (it will be probably useless for short-lived processes). It supports many platforms.You could try writing your own script that could do this. It could get the PID of the launched program and use ps or /proc/[pid]/status to get memory information about it. Of course this wouldn't totally characterize the memory usage but it should give you a good idea.
Just use process accounting (
acct
orpsacct
depending on your distribution). The process accounting log includes "memory used" statistics.