【问题标题】:Is ist possible to get a MemoryMXBean instance with a remote JVM?是否可以使用远程 JVM 获取 MemoryMXBean 实例?
【发布时间】:2009-05-13 09:38:32
【问题描述】:

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

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

【问题讨论】:

    标签: java mbeans


    【解决方案1】:

    this page 所述,您可以使用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
           ...
       }
    

    但是,据我了解,您将无法使用 Java 接口,您需要使用

    查询所需的属性
    CompositeDataSupport mem = (CompositeDataSupport)serv.getAttribute(memory, "NonHeapMemoryUsage") ;
    

    mem.get("committed")
    

    这非常糟糕(“字符串类型”界面,正如他们在另一个问题中所说的那样)。

    正如 Brian Agnew 所说,JConsole 视图对于找出所需信息的存储位置非常有用。

    【讨论】:

    • 谢谢。您解决了我在通过 JMX 使用代理 MemoryMXBean 接口时遇到的强制转换异常。这应该被投票作为答案。您可以通过映射到 MemoryUsage 来扩展您的答案: MemoryUsage memoryUsage = MemoryUsage.from(mem);
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-06-03
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      相关资源
      最近更新 更多