Programatically Querying Values from Garbage Collector Manager MBean

Querying attribute values from some MBean are very easy such as : Available

Object mbeanObjectName =new ObjectName("java.lang:type=OperatingSystem");

Object attribute = _mbsc.getAttribute(mbeanObjectName,"AvailableProcessors");

My question is... Is there an easy way to query values such as

LastGcInfo injava.lang:type=GarbageCollector,name=MarkSweepCompact

Inside this attribute, the value formemoryUsageAfterGcis a type of Map (TablularData). The tablular data contains a key and value pair. I am afterPerm Gen. The value is a CompositeData type.

After drilling all the way in, I arive at the value I am after "used".

Is there an easy way to query this value directly? Maybe through getAttribute?

Or, is the only way to write a lot of code and iterating through the maps and composite data?

[973 byte] By [Shawn@@a] at [2007-11-26 22:36:21]
# 1

Hi,

Use ManagementFactory.newPlatformMXBeanProxy as I have described here:

"How To Retrieve Remote JVM Monitoring And Management Information"

http://blogs.sun.com/jmxetc/entry/how_to_retrieve_remote_jvm

best regards,

-- daniel

JMX, SNMP, Java, etc...

http://blogs.sun.com/jmxetc

dfuchsa at 2007-7-10 11:46:28 > top of Java-index,Core,Monitoring & Management...
# 2

By far the easiest way of dealing with MXBeans programmatically is through MXBean proxies. You can do this:

import com.sun.management.GarbageCollectorMXBean;

import com.sun.management.GcInfo;

...other imports from java.*...

GarbageCollectorMXBean gcm =

ManagementFactory.newPlatformMXBeanProxy(

_mbsc, "java.lang:type=GarbageCollector,name=MarkSweepCompact",

GarbageCollectorMXBean.class);

GcInfo gci = gcm.getLastGcInfo();

Map<String, MemoryUsage> map = gci.getMemoryUsageAfterGc();

MemoryUsage mu = map.get("PS Perm Gen");

long used = mu.getUsed();

It's still a fair amount of code, but you don't need to iterate over anything or look at CompositeData ever.

The com.sun.management interfaces used here are documented at http://java.sun.com/javase/6/docs/jre/api/management/extension/overview-summary.html

Regards,

蒩monn McManus -- JMX Spec Lead -- http://weblogs.java.net/blog/emcmanus

emcmanusa at 2007-7-10 11:46:28 > top of Java-index,Core,Monitoring & Management...
# 3

Thanks for the replies. Duke stars to each of you.

I ended up going a more generic route using JXPath http://jakarta.apache.org/commons/jxpath/ and writing custom DynamicPropertyHandlers for compositedata and tabulardata.

Now I can collect just about any value using three parameters such as

"java.lang:type=GarbageCollector,name=PS MarkSweep", "LastGcInfo", "memoryUsageAfterGc[@name='PS Old Gen']/used"

Shawn@@a at 2007-7-10 11:46:28 > top of Java-index,Core,Monitoring & Management...
# 4
Very nice! I wonder if the JXPath project would be interested in taking your DynamicPropertyHandlers?
emcmanusa at 2007-7-10 11:46:29 > top of Java-index,Core,Monitoring & Management...