如何通过JMX以编程方式访问内存使用情况? [英] How do I access memory usage programmatically via JMX?

查看:117
本文介绍了如何通过JMX以编程方式访问内存使用情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找示例Java JMX代码来访问来自另一个VM的JMX属性的值。

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

使用JConsole,我可以毫不费力地查看java.lang / Memory / Attributes / HeapMemory

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

如何从VM中运行的Java程序中获取相同的信息?

How would I get the same information from a Java program running in a VM?

需要的任何命令行选项的示例,或其他需要启动的内容。

Examples of any command line options needed, or other things that need to be started appreciated.

推荐答案

您需要设置JMXConnector。下面是一个代码片段,它将获取远程计算机上提交的堆内存使用情况。

You need to setup a JMXConnector. Here is a code snippet that will get the committed heap memory usage on a remote machine.

String host ="myHost";
int port = 1234;
HashMap map = new HashMap();
String[] credentials = new String[2];
credentials[0] = user;
credentials[1] = password;
map.put("jmx.remote.credentials", credentials);
JMXConnector c = JMXConnectorFactory.newJMXConnector(createConnectionURL(host, port), map);
c.connect();
Object o = c.getMBeanServerConnection().getAttribute(new ObjectName("java.lang:type=Memory"), "HeapMemoryUsage");
CompositeData cd = (CompositeData) o;
System.out.println(cd.get("committed"));

private static JMXServiceURL createConnectionURL(String host, int port) throws MalformedURLException
{
    return new JMXServiceURL("rmi", "", 0, "/jndi/rmi://" + host + ":" + port + "/jmxrmi");
}

如果您不关心安全性,可以将地图设置为null。您需要启动远程服务器;

If you don't care about security you can set the map to null. You need to start up the remote server with;

-Dcom.sun.management.jmxremote.port=1234
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

您可能需要查看 wlshell ,这是一个允许您使用文本界面或脚本访问远程服务器上的MBean的小实用程序,它可以与WebLogic一起使用,但它适用于已启用远程监视的任何Java程序。

You might want to take a look at wlshell which is a small utility that allows you to access MBeans on a remote server using a text interface or a script, It can be used with WebLogic, but it works for any Java program where you have enabled remote monitoring.

这篇关于如何通过JMX以编程方式访问内存使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆