是否可以通过远程 JVM 获取 MemoryMXBean 实例?

发布于 2024-07-19 02:18:43 字数 293 浏览 4 评论 0原文

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

我的问题是,是否可以轻松地从远程虚拟机获取 MemoryMXBean 类的实例(我该如何做到这一点),或者我是否必须手动查询 MBean?

I found this nice article which explains how to query your current memory for your VM http://recursor.blogspot.com/2006/10/memory-notifications-in-java.html

My question is, is it possible to get an instance of the MemoryMXBean class from a remote VM easily (and how would I do that), or do I have to resort to query the MBeans manually?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢好甜 2024-07-26 02:18:43

此页面< /a> 您可以使用 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") ;

and

mem.get("committed")

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

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

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.

陈年往事 2024-07-26 02:18:43

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

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

You can query JMX beans remotely. See the JMX Connectors section in the JMX tutorial.

The straightforward approach may be to use JConsole to determine what you want to query (in this case your MemoryMXBean) and then code around that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文