Skip to content

Instantly share code, notes, and snippets.

@cowlicks
Last active November 10, 2021 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowlicks/c7fdefe7639ec116a31f2933cc4e7861 to your computer and use it in GitHub Desktop.
Save cowlicks/c7fdefe7639ec116a31f2933cc4e7861 to your computer and use it in GitHub Desktop.
Log memory of a process
# Prints the time the log spans and how many bytes per second its memory has increased
import sys
logname = sys.argv[1]
with open(logname) as f:
first, *_, last = f
t0, m0 = map(int, first.split());
t1, m1 = map(int, last.split());
print(f'Time: [{(t1 - t0)/60:.2f}] at {((m1 - m0)/(t1 - t0)):.2f} B/s')
1636570520 12796
1636570521 12796
1636570522 13060
1636570523 13060
1636570525 13060
1636570526 13060
1636570527 13060
1636570528 13060
1636570529 13060
#logs to a file with format
# seconds-since-epoch memory-used
# you can monitor the file with `tail -f $LOG` from another shell
LOG=log-name
CMDNAME=command-name
rm -f $LOG
while true; do
mem=$(ps -C $CMDNAME --no-headers -o rss) && echo "$(date +%s) $mem" >> $LOG
sleep 1
# uncomment to print memory usage live
# python analyze-memlog.py $LOG
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment