Last active
July 25, 2023 12:49
-
-
Save joshlk/ebe48cc579f986a50ea6f080e1b912e6 to your computer and use it in GitHub Desktop.
Measure the peak memory of the system while a command is running (this only includes physical memory and not virtual memory)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# peak_memory [command ...] | |
# Run the given command line in the background and kill if script exits | |
trap 'kill $(jobs -p) 2&> /dev/null' EXIT | |
"$@" & | |
pid=$! phys_peak=0 not_avail_peak=0 | |
# while command still running | |
while ps -p $pid &>/dev/null; do | |
sleep 1 | |
mem_total="$(cat /proc/meminfo | grep 'MemTotal:' | grep -oe '\([0-9.]*\)')" | |
mem_free="$(cat /proc/meminfo | grep 'MemFree:' | grep -oe '\([0-9.]*\)')" | |
mem_avail="$(cat /proc/meminfo | grep 'MemAvailable:' | grep -oe '\([0-9.]*\)')" | |
phys=$(($mem_total - $mem_free)) | |
not_avail=$(($mem_total - $mem_avail)) | |
let phys_peak='phys > phys_peak ? phys : phys_peak' | |
let not_avail_peak='not_avail > not_avail_peak ? not_avail : not_avail_peak' | |
done | |
phys_peak=$(($phys_peak / 1048576)) | |
not_avail_peak=$(($not_avail_peak / 1048576)) | |
printf '%s %s\n' "$(date)" "Peak Used Physical memory: $phys_peak GiB" 1>&2 | |
printf '%s %s\n' "$(date)" "Peak Used Not-Available memory (exc. mmap and cache): $not_avail_peak GiB" 1>&2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: