JMXServiceURL url = new JMXServiceURL(process.getProcessJmxUrl());
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
return jmxc.getMBeanServerConnection();
}
public ProcessGarbageCollectionStats collect() {
ProcessGarbageCollectionStats gcStats =
new ProcessGarbageCollectionStats();
if ( connection != null) {
try {
Set<ObjectInstance> mbeans=
connection.queryMBeans(new ObjectName("java.lang:type=GarbageCollector,*"),null );
Long totalCollectionCount= new Long(0);
Long totalCollectionTime=new Long(0);
for( ObjectInstance gcObject : mbeans) {
String gcCollectorName = gcObject.getObjectName().getCanonicalKeyPropertyListString();
ObjectName memoryManagerMXBean =
new ObjectName("java.lang:" + gcCollectorName);
totalCollectionCount =+ (Long) connection.getAttribute(memoryManagerMXBean,"CollectionCount");
totalCollectionTime =+ (Long) connection.getAttribute(memoryManagerMXBean,"CollectionTime");
}
// Returns the total number of collections that have occurred.
gcStats.setCollectionCount(totalCollectionCount);
// Returns the approximate accumulated collection elapsed time in milliseconds.
gcStats.setCollectionTime(totalCollectionTime);
} catch( Exception e) {
logger.error("", null, "Failed to Fetch JMX GC Stats From PID:"+process.getPID()+" Reason:\n"+e);
}