Skip to content

Instantly share code, notes, and snippets.

@alexheretic
Created May 19, 2023 15:50
Show Gist options
  • Save alexheretic/a235f89636d727a55c5a33cd6f087fda to your computer and use it in GitHub Desktop.
Save alexheretic/a235f89636d727a55c5a33cd6f087fda to your computer and use it in GitHub Desktop.
Output the memory usage of a pid and measure the change.
#!/usr/bin/env bash
## Output the memory usage of a pid and measure the change.
set -euo pipefail
pid=$1
last_usage_mb=0
while true; do
mem_usage=$(ps -p "$pid" -o size=)
usage_mb=$(bc <<< "scale = 2; $mem_usage / 1024")
change_mb=$(bc <<< "scale = 2; $usage_mb - $last_usage_mb")
if [[ $(bc <<< "$change_mb > 0") == 1 ]]; then
change_mb="+$change_mb"
fi
last_usage_mb="$usage_mb"
if [[ $usage_mb != 0 ]]; then
echo -ne "\r \r"
echo -n "Mem: $usage_mb MiB ($change_mb MiB/s)"
fi
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment