Measuring RAM usage of a program
time
is a built-in of your shell. If you like time
but need more information, try GNU time
in verbose (-v
) mode:
/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.
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
).
Maybe overkill, but I just found that valgrind
has a nice tool named massif
. I tested it on xterm
:
valgrind --trace-children=yes --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=yes
the option is there!