How do I access memory usage programmatically via JMX?

后端 未结 4 1700
[愿得一人]
[愿得一人] 2020-11-29 11:28

I\'m looking for sample Java JMX code to access the values of JMX attributes from another VM.

With JConsole, I have no problem looking at java.lang/Memory/Attributes

4条回答
  •  猫巷女王i
    2020-11-29 11:28

    @Kire's answer looks good but I thought I'd add some details about my SimpleJMX package. It contains server support that allows you to export beans easily and also includes a simple client interface that works against any JVM that exports JMX information.

    To access memory usage you'd do something like:

    JmxClient client = new JmxClient("some.host.name", somePortNumber);
    // get the memory composite information
    CompositeData composite =
          (CompositeData)client.getAttribute(new ObjectName("java.lang:type=Memory"),
                                             "HeapMemoryUsage");
    System.out.println(composite.get("committed"));
    

提交回复
热议问题