6

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.

user2869231
  • 1,301
  • 4
  • 21
  • 46
  • 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? – Victor Sep 30 '14 at 22:02
  • sounds like it SHOULD work. However, I have no knowledge of how to do it :/ – user2869231 Sep 30 '14 at 22:04

1 Answers1

0

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.

Community
  • 1
  • 1
Victor
  • 2,133
  • 2
  • 21
  • 47