Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers

up vote 13 down vote accepted

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).

share|improve this answer
    
I like it. It even did The Right Thing with ./tstime -t bash -c 'perl -e "\$a=\"x\"x100000000;\$b=\$a.\$a;\$b=\"\";\$a=\"\";sleep 10;"' –  Ole Tange Aug 22 '11 at 12:00
    
"RAM usage by the process" isn't a well-defined value: If there are several instances of the same program running, they share the executable. Most programs share glibc (and other assorted libraries, they are called "shared" for something). Many daemons load configuration into memory and fork(2) children, who then share the configuration data. Then there is data in readahead/writebehind buffers managed by the kernel. And then there are services that are a herd of loosely coupled processes (think of your desktop environment and all its applets and background stuff). –  vonbrand Jan 23 '13 at 16:05
    
@vonbrand, how the Linux-Kernel computes the RSS/VSS values is well-defined. –  maxschlepzig Jan 23 '13 at 19:46
    
@maxschlepzig, it may well compute some random values, that doesn't mean they mean what you think they mean: The resident set is just the pages in the process' address space that are currently in memory. That is not the "memory used by this process", it includes whatever it is sharing. –  vonbrand Jan 23 '13 at 19:50
add comment

Maybe overkill, but I just found that valgrind has a nice tool named massif. I tested it on xterm:

valgrind --trace-children=true --tool=massif xterm
ms_print massif.out.* | less

And you obtain a nice memory usage graph:

    MB
4.230^                     #                    :::::::  :::      @@:     ::: 
     |   @                 #:::@::@@:::::@::::::: :: : ::: :::::::@ ::::::: ::
     |   @               ::#:: @: @ ::: :@: :: :: :: : ::: ::: : :@ :: : :: ::
     |   @::::@@:::::::::: #:: @: @ ::: :@: :: :: :: : ::: ::: : :@ :: : :: ::
     |   @::: @ :: ::: : : #:: @: @ ::: :@: :: :: :: : ::: ::: : :@ :: : :: ::
     |   @::: @ :: ::: : : #:: @: @ ::: :@: :: :: :: : ::: ::: : :@ :: : :: ::
     |   @::: @ :: ::: : : #:: @: @ ::: :@: :: :: :: : ::: ::: : :@ :: : :: ::
     |   @::: @ :: ::: : : #:: @: @ ::: :@: :: :: :: : ::: ::: : :@ :: : :: ::
     |   @::: @ :: ::: : : #:: @: @ ::: :@: :: :: :: : ::: ::: : :@ :: : :: ::
     |   @::: @ :: ::: : : #:: @: @ ::: :@: :: :: :: : ::: ::: : :@ :: : :: ::
   0 +----------------------------------------------------------------------->Mi
     0                                                                   292.4

along with overly detailed memory usage info. Details in the valgrind manual.

Programs will run about 20x slower though. Also, I ran some commands inside the xterm. Their memory footprint has been taken into account because the --trace-children=true the option is there!

share|improve this answer
    
Th 20x speed penalty makes it unsuable for my situation. Otherwise very pretty graph! –  Ole Tange Aug 22 '11 at 11:59
add comment

time is a built-in of your shell. If you like time but need more information, try GNU time.

/usr/bin/time -v sleep 5               
    Command being timed: "sleep 5"
    User time (seconds): 0.00
    System time (seconds): 0.00
    Percent of CPU this job got: 0%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:05.00
    Average shared text size (kbytes): 0
    Average unshared data size (kbytes): 0
    Average stack size (kbytes): 0
    Average total size (kbytes): 0
    Maximum resident set size (kbytes): 2144
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 179
    Voluntary context switches: 2
    Involuntary context switches: 1
    Swaps: 0
    File system inputs: 0
    File system outputs: 0
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0

Search the package "time" or "gnutime" in your package manager.

share|improve this answer
3  
Note that some information reported by GNU time could be inaccurate. E.g. under Ubuntu 10.04: The man page states 'The numbers are only as good as those returned by wait3(2)'. I.e. wait3 fills a structure which is described in getrusage(2): 'Not all fields are meaningful under Linux. [..]'. –  maxschlepzig Aug 16 '11 at 19:49
2  
For example on a test program that exactly allocates 10 MB (and touches each page) - GNU time reports a maxRSS of 42608 KiB - and tstime reports 10652 KiB. Again under Ubuntu 10.04. –  maxschlepzig Aug 16 '11 at 19:58
    
Thanks for the info. –  Juan Francisco Cantero Hurtado Aug 16 '11 at 21:55
    
I would have loved if it was this simple. On my Ubuntu machine I tried: /usr/bin/time -v perl -e '$a="x"x100000000;$b=$a.$a;sleep 10;'. top says it takes around 570 MB, but time says 2.3 GB. In practice that number is not usuable to me. –  Ole Tange Aug 22 '11 at 11:56
    
The factor of 4 is fixed in GNU time 1.7 and thus works as expected. –  Ole Tange Jan 30 '13 at 13:16
add comment

Looks like tstime doesn't work anymore under non-root under Linux >=3.0. Here's a polling utility I wrote to hack around the issue: https://github.com/jhclark/memusg/blob/master/memusg

share|improve this answer
    
/usr/bin/time -v gives the correct output in newer versions. In older versions you just need to divide by 4 to get the correct amount. –  Ole Tange Aug 30 '12 at 22:42
    
However, I don't think time -v supports peak vmemory size (only RSS). Can anyone confirm this on the latest version? –  jhclark Sep 1 '12 at 0:48
add comment

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.