tags:

views:

195

answers:

2

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?

+1  A: 

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.

Brian Agnew
A: 

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.

Kevin