Skip to content

Instantly share code, notes, and snippets.

@balaji-katika
Created June 12, 2015 10:33
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 balaji-katika/1d7a288b4f9dec6f4a11 to your computer and use it in GitHub Desktop.
Save balaji-katika/1d7a288b4f9dec6f4a11 to your computer and use it in GitHub Desktop.
Shell script to measure the java heap memory usage over the execution of the JVM
#!/bin/sh
#
# Description:
# This program shall display the statistics about the usage of the heap
# by the JVM during the execution of this program
#
## Functions ##
function show_help
{
echo "Usage:"
echo " sh heap_usage.sh <vmid> [interval] [count]"
echo " vmid - Virtual Machine Identifier, string that indicates the target JVM"
echo " interval - Sampling interval in seconds"
echo " count - Number of samples to display"
exit 1;
}
function update_result
{
#Update max value
if [[ $1 -ge $max ]]
then
max=$1
fi
#Update min value
if (( ($min == 0) || ($1 < $min) ))
then
min=$1
fi
}
## Main ##
if [[ $# -le 0 ]]
then
show_help
fi
# variable
jvmid=$1
interval=0.3
count=100
i=0
min=0
max=0
if [ -n "$2" ]
then
interval=$2
fi
if [ -n "$3" ]
then
count=$3
fi
while [[ $i -le $count ]]
do
dummy=`jps | grep $jvmid | head -1 | cut -d' ' -f1`
if [[ $dummy -eq "" ]]
then
echo "Exiting..."
break
fi
result=`jmap -heap $jvmid | grep "^\s*used" | awk -F' ' '{ sum += $3 } END { print sum }'`
echo $result
update_result $result
sleep $interval
((i++))
done
echo "Results Summary"
#max=max/1024/1024
max=`expr $max / 1024`
max=`expr $max / 1024`
min=`expr $min / 1024`
min=`expr $min / 1024`
echo " Max Heap Usage: $max MB"
echo " Min Heap Usage: $min MB"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment