Get the JVM heap utilisation for a running WebSphere server
I recently had to find out the amount of heap memory (the place a JVM stores it's object instances) being used by a running WebSphere v7.0 instance we suspected of being close to out of memory.
To do this, I could use the below jython script called 'showHeapSize.py':
serverJVM=AdminControl.completeObjectName('type=JVM,process=' + sys.argv[0] + ',*')
serverJVMObj=AdminControl.makeObjectName(serverJVM)
perf=AdminControl.completeObjectName('type=Perf,process=' + sys.argv[0] + ',*')
perfObj=AdminControl.makeObjectName(perf)
jvmObj=AdminControl.invoke_jmx(perfObj,'getStatsObject',[serverJVMObj,java.lang.Boolean('false')],['javax.management.ObjectName','java.lang.Boolean'])
currentHeapsize=jvmObj.getStatistic('HeapSize').getCurrent()
usedMemory=jvmObj.getStatistic('UsedMemory').getCount()
usage=float(usedMemory)/float(currentHeapsize)*100
print sys.argv[0] + ".> "+str(currentHeapsize)+"K .> "+str(usedMemory)+"K .> "+"Usage:%.2f" % usage+"%"
Which can then be executed as follows:
/opt/WebSphere/7.0/AppServer/profiles/AppSrv01/bin/wsadmin.sh -lang jython -f showHeapSize.py server1
where "AppSrv01" is your profile name and "server1" is the server name - producing the following output:
server1 .> 893760K .> 483776K .> Usage:54.13%
Remember that WebSphere allows you to set an Initial as well as a Maximum heap size - if they're not the same then the heap size could be anywhere between these two values. The output above will show you the percentage use of the *current* heapsize, which may not be the maximum. Some recommend setting these to the same value to remove the performance overhead of the JVM needed to allocate more memory as it grows the heap.