我发现这篇很好的文章解释了如何为你的虚拟机查询当前内存http://recursor.blogspot.com/2006/10/memory-notifications-in-java.html
我的问题是,是否可以轻松地从远程 VM 获取 MemoryMXBean 类的实例(以及我将如何做到这一点),还是我必须求助于手动查询 MBean?
我发现这篇很好的文章解释了如何为你的虚拟机查询当前内存http://recursor.blogspot.com/2006/10/memory-notifications-in-java.html
我的问题是,是否可以轻松地从远程 VM 获取 MemoryMXBean 类的实例(以及我将如何做到这一点),还是我必须求助于手动查询 MBean?
as described on this page you can access it remotly, with the MBeanServerConnection
:
MBeanServerConnection mbs;
// Connect to a running JVM (or itself) and get MBeanServerConnection
// that has the JVM MXBeans registered in it
...
try {
// Assuming the RuntimeMXBean has been registered in mbs
ObjectName oname = new ObjectName(ManagementFactory.RUNTIME_MXBEAN_NAME);
// Get standard attribute "VmVendor"
String vendor = (String) mbs.getAttribute(oname, "VmVendor");
} catch (....) {
// Catch the exceptions thrown by ObjectName constructor
// and MBeanServer.getAttribute method
...
}
however, as far as I understand, you won't be able to use the Java interface, you'll need to query the properties you want with
CompositeDataSupport mem = (CompositeDataSupport)serv.getAttribute(memory, "NonHeapMemoryUsage") ;
and
mem.get("committed")
which is quite awfull ('stringly-typed' interface, as they said in another question).
As Brian Agnew said, the JConsole view is very usefull to find out where the information you want is stored.