Java 如何通过 JMX 以编程方式访问内存使用情况?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1759831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I access memory usage programmatically via JMX?
提问by ThoughtfulHacking
I'm looking for sample Java JMX code to access the values of JMX attributes from another VM.
我正在寻找示例 Java JMX 代码以从另一个 VM 访问 JMX 属性的值。
With JConsole, I have no problem looking at java.lang/Memory/Attributes/HeapMemory
使用 JConsole,我可以毫无问题地查看 java.lang/Memory/Attributes/HeapMemory
How would I get the same information from a Java program running in a VM?
我如何从在 VM 中运行的 Java 程序获取相同的信息?
Examples of any command line options needed, or other things that need to be started appreciated.
需要的任何命令行选项的示例,或需要启动的其他内容。
采纳答案by Kire Haglin
You need to setup a JMXConnector. Here is a code snippet that will get the committed heap memory usage on a remote machine.
您需要设置一个 JMXConnector。这是一个代码片段,它将获取远程机器上提交的堆内存使用情况。
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");
}
If you don't care about security you can set the map to null. You need to start up the remote server with;
如果您不关心安全性,您可以将地图设置为 null。您需要启动远程服务器;
-Dcom.sun.management.jmxremote.port=1234
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
You might want to take a look at wlshellwhich 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.
您可能想看看wlshell,它是一个小实用程序,允许您使用文本界面或脚本访问远程服务器上的 MBean,它可以与 WebLogic 一起使用,但它适用于您启用的任何 Java 程序远程监控。
回答by Adamski
// Retrieve memory managed bean from management factory.
MemoryMXBean memBean = ManagementFactory.getMemoryMXBean() ;
MemoryUsage heap = memBean.getHeapMemoryUsage();
MemoryUsage nonHeap = memBean.getNonHeapMemoryUsage();
// Retrieve the four values stored within MemoryUsage:
// init: Amount of memory in bytes that the JVM initially requests from the OS.
// used: Amount of memory used.
// committed: Amount of memory that is committed for the JVM to use.
// max: Maximum amount of memory that can be used for memory management.
System.err.println(String.format("Heap: Init: %d, Used: %d, Committed: %d, Max.: %d",
heap.getInit(), heap.getUsed(), heap.getCommitted(), heap.getMax()));
System.err.println(String.format("Non-Heap: Init: %d, Used: %d, Committed: %d, Max.: %d",
nonHeap.getInit(), nonHeap.getUsed(), nonHeap.getCommitted(), nonHeap.getMax()));
回答by Gray
@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.
@Kire 的答案看起来不错,但我想我会添加一些有关SimpleJMX 包的详细信息。它包含允许您轻松导出 bean 的服务器支持,还包含一个简单的客户端界面,该界面适用于任何导出 JMX 信息的 JVM。
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"));
回答by Juan Bustamante
This is how you get the MemoryMXBean remotely (to complement @Adamski's answer):
这是您远程获取 MemoryMXBean 的方式(以补充@Adamski 的回答):
MemoryMXBean memoryMXBeanProxy = JMX.newMXBeanProxy(
conn, new ObjectName("java.lang:type=Memory"), MemoryMXBean.class);