While trying to use GarbageCollectorMXBean to obtain GC information about a running process, I had to look around to find out the correct name strings for various Garbage Collectors.
This is what I did to obtain an instance of GarbageCollectorMXBean:
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
GarbageCollectorMXBean bean = ManagementFactory.newPlatformMXBeanProxy(server,
where GC_BEAN_NAME is the ObjectName for uniquely identifying the MXBean for a garbage collector within an MBeanServer and it is of the form:
Now, what is the correct string for collector's name?
Here are the name strings for various collectors in VM:
You can run a simple java program with different collectors and using the below code snippet, can see the full object names of all the available GarbageCollectorMXBean MXBeans.
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
Set names = mbs.queryNames(null, null);
System.out.println(names.toString().replace(", ", System.getProperty("line.separator")));