Measuring maximum RAM usage with Bash on Linux
While running a simulation on my University's Viper HPC, I found that I needed to measure the maximum RAM usage of a simulation that I was running. Since the solution wasn't particularly easy to find, I thought I'd quickly blog about it here.
Doing this isn't actually as easy as you might think. In the end, I used this:
/usr/bin/time --format 'Max RAM working set size: %Mk' command_here --foo bar
....replacing command_here --foo bar
with the command you want to measure.
/usr/bin/time
is a program that measures how long a command takes to execute, but it's evident that it measures a bunch of other different things as well. While the output format leaves something to be desired (hence the --format
in the above), it does the job pretty well.
Note that /usr/bin/time
is distinct from the time
built-in you get in Bash. Depending on your shell, you may need to explicitly specify the full path to the time
binary. In addition, if your system doesn't have the command (like Viper), you may need to copy it from another system that does that has the same CPU architecture.
I forget where it was that I found this solution, but if you comment below then I'll add the credit to this post if your post looks familiar.
As a quick extra, you can limit the time a command can execute for like this:
timeout 60 command_here
That will limit command_here
to executing for only 60 seconds.
Found this interesting? Comment below!