brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: /usr/bin/time -f '%e s, %M KB' shows how much memory a command needed

Linux
~/tips/tip-performance-gnu-time-peak-memory

/usr/bin/time -f "%e s elapsed, %M KB peak memory" tar czf /tmp/test.tar.gz ~/Documents

Reports elapsed time and peak memory of one command with GNU time.

The time you type in bash only tells you how long something took. There is a second one, GNU time, that also reports memory, page faults and I/O, and I reach for it when I want to know how big a job is before I give it to a small machine.

Bash has a built-in time that takes priority, so call the real program by its path:

/usr/bin/time -f "%e s elapsed, %M KB peak memory" tar czf /tmp/test.tar.gz ~/Documents

The manual describes %e as the elapsed real time in seconds and %M as the maximum resident set size of the process during its lifetime, in kilobytes. Other useful fields are %U and %S for user and system CPU seconds, %P for the CPU share, %F for major page faults (pages that had to be read from disk), %I and %O for filesystem inputs and outputs, and %x for the exit status. For everything it knows, use -v.

Add -o results.txt -a to write the report to a file and append to it instead of overwriting, so you can compare several runs.

The caveats from the man page: not all resources are measured by every version of UNIX, so some values can be zero. GNU time also writes its report to standard error, and the man page says the exit status is that of your command.

Further reading: time(1) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link