Getting Java VisualVM data from the command line
Asked Answered
J

1

7

I'll start off with saying that I have just about no experience with Java VisualVM. However, it contains the information that some developers would like to see. When I open it up for my application, it contains a graph for CPU, Memory, Classes, and Threads. I was wondering if there was a way you could grab that information from the command line. So, if the application was using up 250 MB of memory at the time of call, is there a command I could write that would return 250 MB? Likewise with the number of threads it is using?

The version I'm using is 1.7.0_51.

Thanks.

Jeri answered 30/9, 2014 at 21:58 Comment(2)
VisualVM is just a client application that consumes information exposed by the JVM via JMX. You can easily develop a cilent application that queries those JMX beans and retrieve the information you need, and then invoke it via command line. Would that be ok for you?Meyers
sounds like it SHOULD work. However, I have no knowledge of how to do it :/Jeri
M
1

VisualVM is just a client application that consumes information exposed by the JVM via JMX. If you want to develop a quick client application and then invoke it via command line, is very easy:

Open a connection to the JVM (note that it needs to have the JMX ports open) using a URL:

final JMXServiceURL jmxUrl = new JMXServiceURL(jmxServiceUrl);
final JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxUrl);
final MBeanServerConnection mbsc = jmxConnector.getMBeanServerConnection();

Then, use the MBeanServerConnection object to perform queries on the JMX Beans exposed by the JVM. Sample about memory:

ObjectName jvmMemory = new ObjectName("java.lang", "type", "Memory");           
CompositeData heapUsage = (CompositeData) mbsc.getAttribute(jvmMemory, "HeapMemoryUsage");
printer.print(String.valueOf(heapUsage.get("used")));
printer.print(String.valueOf(heapUsage.get("committed")));
printer.print(String.valueOf(heapUsage.get("max")));

You have a whole range of Mbeans to query. Use the JVisualVM to see what are those MBeans.

Update

For info on how to open the JMX ports, see this answer.

Meyers answered 30/9, 2014 at 22:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.