Skip to content

Instantly share code, notes, and snippets.

@jffry
Created January 10, 2017 16:22
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 jffry/7d5cf3cc285b62485e529cca66f1b1c5 to your computer and use it in GitHub Desktop.
Save jffry/7d5cf3cc285b62485e529cca66f1b1c5 to your computer and use it in GitHub Desktop.
Log memory usage of Java every second as it's running
#!/bin/bash
#helper that gives the current unix time, in seconds
function now() {
date -u "+%s"
}
#helper that calculates elapsed ms
START="$(now)"
function elapsed() {
NOW="$(now)"
echo $((NOW-START))
}
#helper that prints a single line of stats about latest java proc
function stats() {
pid=$(pgrep java | tail -n 1)
if [ -z "$pid" ] ; then
echo "$(elapsed), 0, 0"
else
echo "$(elapsed), $pid, $(ps -o rss= -p $pid)"
fi
}
#main loop
echo "elapsed-sec, proc-id, mem-rss-kb"
while [ 1 ] ; do
stats
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment