2

我发现这篇很好的文章解释了如何为你的虚拟机查询当前内存http://recursor.blogspot.com/2006/10/memory-notifications-in-java.html

我的问题是,是否可以轻松地从远程 VM 获取 MemoryMXBean 类的实例(以及我将如何做到这一点),还是我必须求助于手动查询 MBean?

4

2 回答 2

2

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.

于 2010-07-01T16:27:02.840 回答
1

您可以远程查询 JMX bean。请参阅 JMX 教程中的JMX 连接器部分。

直接的方法可能是使用JConsole来确定您想要查询的内容(在本例中是您的 MemoryMXBean),然后围绕它编写代码。

于 2009-05-13T10:41:24.187 回答