Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access memory usage programmatically via JMX?

Tags:

java

jmx

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/HeapMemory

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.

like image 801
ThoughtfulHacking Avatar asked Nov 18 '09 23:11

ThoughtfulHacking


People also ask

What is JMX memory?

Description: The memory system of the Java virtual machine including both the heap and non-heap memory.

How do I monitor heap usage?

The easy way to monitor Heap usage is by using a commercial APM (Application Performance management tool) such as CA Wily APM, AppDynamics, New Relic, Riverbed, etc. APM tools not only monitor the heap usage, but you can also configure the tool to Alert you when Heap usage is not normal.


2 Answers

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");
}

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

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.

like image 84
Kire Haglin Avatar answered Sep 28 '22 04:09

Kire Haglin


@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"));
like image 40
Gray Avatar answered Sep 28 '22 04:09

Gray