RAM Usage – Measuring RAM Usage of a Program

measurememoryprocess

time is a brilliant command if you want to figure out how much CPU time a given command takes.

I am looking for something similar that can measure the max RAM usage of the program and any children. Preferably it should distinguish between allocated memory that was used and unused. Maybe it could even give the median memory usage (so the memory usage you should expect when running for a long time).

So I would like to do:

rammeassure my_program my_args

and get output similar to:

Max memory allocated: 10233303 Bytes
Max memory used: 7233303 Bytes
Median memory allocation: 5233303 Bytes

I have looked at memusg https://gist.github.com/526585/590293d6527c91e48fcb08edb8de9fd6c88a6d82 but I regard that as somewhat a hack.

Best Answer

You can use tstime to measure the highwater memory usage (RSS and virtual) of a process.

For example:

$ tstime date       
Tue Aug 16 21:35:02 CEST 2011

Exit status: 0

pid: 31169 (date) started: Tue Aug 16 21:35:02 2011
        real   0.017 s, user   0.000 s, sys   0.000s
        rss      888 kb, vm     9764 kb

It also supports a more easy to parse output mode (-t).

Related Question