How do I access memory usage programmatically via JMX?

Viewed 41248

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.

4 Answers

This is how you get the MemoryMXBean remotely (to complement @Adamski's answer):

MemoryMXBean memoryMXBeanProxy = JMX.newMXBeanProxy(
            conn, new ObjectName("java.lang:type=Memory"), MemoryMXBean.class);
Related