Skip to content

Instantly share code, notes, and snippets.

@krishnamurthydasari
Last active February 20, 2018 13:44
Show Gist options
  • Save krishnamurthydasari/df9ad09ed59131de878999ee3cb98693 to your computer and use it in GitHub Desktop.
Save krishnamurthydasari/df9ad09ed59131de878999ee3cb98693 to your computer and use it in GitHub Desktop.
jmap needs openjdk-debuginfo package. Install and try below command for heap usage
yum list *openjdk-debuginfo* --enablerepo=*
yum install java-1.8.0-openjdk-debuginfo.x86_64 --enablerepo=amzn-updates-debuginfo
-----
From below output counting all used entries will get actual heap usage. Note that New Generation includes Eden and 1 survivor space. So in provided scripts below i have removed first entry for new generation and considered rest all.
[root@ip-50-1-0-167 jvm]# jmap -heap 13621
Attaching to process ID 13621, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.91-b14
using parallel threads in the new generation.
using thread-local object allocation.
Concurrent Mark-Sweep GC
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 536870912 (512.0MB)
NewSize = 178913280 (170.625MB)
MaxNewSize = 178913280 (170.625MB)
OldSize = 89522176 (85.375MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 21807104 (20.796875MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 268435456 (256.0MB)
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
New Generation (Eden + 1 Survivor Space):
capacity = 161021952 (153.5625MB)
used = 134721432 (128.4803695678711MB)
free = 26300520 (25.082130432128906MB)
83.66650032909799% used
Eden Space:
capacity = 143130624 (136.5MB)
used = 134546904 (128.31392669677734MB)
free = 8583720 (8.186073303222656MB)
94.00287670093579% used
From Space:
capacity = 17891328 (17.0625MB)
used = 174528 (0.16644287109375MB)
free = 17716800 (16.89605712890625MB)
0.9754893543956044% used
To Space:
capacity = 17891328 (17.0625MB)
used = 0 (0.0MB)
free = 17891328 (17.0625MB)
0.0% used
concurrent mark-sweep generation:
capacity = 272904192 (260.26171875MB)
used = 227350752 (216.81857299804688MB)
free = 45553440 (43.443145751953125MB)
83.30790023188797% used
28317 interned Strings occupying 3281168 bytes.
===============
SCRIPTS
===============
[root@ip-20-1-0-13 krishna]# more heapusage.sh
#!/bin/bash
PID=`pgrep java`
DATE=`date +%Y-%m-%d" "%H":"%M":"%S`
HEAP=`/usr/bin/jmap -heap $PID |grep "used =" |tail -n +2 |awk '{sum+=$3} END {print sum/1024/1024}'`
echo "$DATE,$HEAP" >> heapusage.csv
============================
[root@ip-20-1-0-13 krishna]# more memusage.sh
#!/bin/bash
DATE=`date +%Y-%m-%d" "%H":"%M":"%S`
MEM=`free -m |grep Mem |awk '{print $3}'`
echo "$DATE,$MEM" >> memusage.csv
#echo "`date +%Y-%m-%d" "%H":"%M":"%S`,`free -m |grep Mem |awk '{print $3}'`" >> memusage.csv
=====================
@krishnamurthydasari
Copy link
Author

Same script in Windows:

#aws cloudwatch put-metric-data --metric-name "JVMHeapSize" --namespace "JVMCustMetrics" --dimensions "Name=Server,Value=server001" --value "$heapsize" --unit "Count"

$heapsize = javaw -XX:+PrintFlagsFinal | findstr "MaxHeapSize"
$heapSizeSplit = ((($heapsize -split ':=')[1]) -split '{' )[0]
Write-Host ($heapSizeSplit)
$webclient = new-object net.webclient
$instanceid = $webclient.Downloadstring('http://169.254.169.254/latest/meta-data/instance-id')
Write-Host ($instanceid)
$metricunit="Bytes"
$hname = hostname
Write-Host ($hname)
$timestamp= [DateTime]::UtcNow
Write-Host ($timestamp)
$currTime = Get-Date -UFormat "%Y / %m / %d / %A / %Z"
$memjson="[{"MetricName": "Heap Size","Timestamp": "$timestamp", "Value": "$heapSizeSplit","Unit": "$metricunit", "Dimensions":[{ "Name" : "InstanceId", "Value" : "$instanceid" },{ "Name" : "Hostname", "Value" : "$hname" } ] }]"
#$memjson="[{"MetricName": "Heap Size","Timestamp": "$timestamp", "Value": $heapSizeSplit,"Unit": "$metricunit", "Dimensions":[{ "Name" : "InstanceId", "Value" : $instanceid },{ "Name" : "Hostname", "Value" : $hname } ] }]"
echo $memjson > memjson.js
#aws cloudwatch put-metric-data --region us-west-2 --namespace "JVMMetrics" --metric-data file://memjson.js
aws cloudwatch put-metric-data --region "us-west-2" --metric-name "JVMHeapSize" --namespace "JVMCustMetrics" --dimensions "Name=InstanceId,Value=$instanceid" --value "$heapSizeSplit" --unit "Bytes"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment