是否可以使用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如何绘制内存图,源代码的任何指针?
答案 0 :(得分:4)
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());
答案 1 :(得分:3)
用于获取本地/远程tomcat的已用内存的代码片段:
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://{remote ip/localhost}:2020/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url);
MBeanServerConnection server = jmxc.getMBeanServerConnection();
Object o = jmxc.getMBeanServerConnection().getAttribute(new ObjectName("java.lang:type=Memory"), "HeapMemoryUsage");
CompositeData cd = (CompositeData) o;
System.out.println(cd.get("used"));
答案 2 :(得分:1)
示例代码
列出memBeans = //获取mbeans列表 for(Iterator i = memBeans.iterator(); i.hasNext();){
MemoryPoolMXBean mpool = (MemoryPoolMXBean)i.next();
MemoryUsage usage = mpool.getUsage();
String name = mpool.getName();
float init = usage.getInit()/1000;
float used = usage.getUsed()/1000;
float committed = usage.getCommitted()/1000;
float max = usage.getMax()/1000;
float pctUsed = (used / max)*100;
float pctCommitted = (committed / max)*100;
}