如何使用JMX API获取tomcat 7的内存使用情况?

是否可以使用JMX API获取tomcat服务器的内存使用情况统计信息.哪个Mbean可以提供这个信息?我在下面的代码中陷入了ObjectName的形成

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:2020/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url);
MBeanServerConnection server = jmxc.getMBeanServerConnection();

  Object o = jmxc.getMBeanServerConnection().getAttribute(
          new ObjectName("-----"); 

想知道jconsole如何绘制内存图,源代码的任何指针?

解决方法:

MBeanServer connection = ManagementFactory.getPlatformMBeanServer();
Set<ObjectInstance> set = connection.queryMBeans(new ObjectName("java.lang:type=Memory"), null);
ObjectInstance oi = set.iterator().next();
// replace "HeapMemoryUsage" with "NonHeapMemoryUsage" to get non-heap mem
Object attrValue = connection.getAttribute(oi.getObjectName(), "HeapMemoryUsage");
if( !( attrValue instanceof CompositeData ) ) {
    System.out.println( "attribute value is instanceof [" + attrValue.getClass().getName() +
            ", exitting -- must be CompositeData." );
    return;
}
// replace "used" with "max" to get max
System.out.println(((CompositeData)attrValue).get("used").toString());
上一篇:java-发出JMX通知的最佳实践


下一篇:利用JMX,在 zabbix 上监控多 agent 端的 tomcat 实战