Example of ManagementFactory in Java
March 25, 2013
ManagementFactory belongs to the package java.lang.management. java.lang.management has the factory methods. These are static methods. ManagementFactory handles managed beans in java plateform. ManagementFactory provides the method to return MXBean. Below is the sample usage of
ManagementFactory.
ManagementFactoryTest.java
package com.concretepage; import java.lang.management.ClassLoadingMXBean; import java.lang.management.ManagementFactory; import java.lang.management.MemoryPoolMXBean; import java.lang.management.MemoryUsage; import java.lang.management.ThreadMXBean; import java.util.List; public class ManagementFactoryTest { public static void main(String[] a){ Thread th= new ManagementFactoryTest().new UserThread(); Runtime.getRuntime().addShutdownHook(th); } class UserThread extends Thread { public void run() { // Using getThreadMXBean System.out.println("result of getThreadMXBean "); ThreadMXBean thMxB = ManagementFactory.getThreadMXBean(); System.out.println(("Current thread count:"+ thMxB.getThreadCount())); //using getMemoryPoolMXBeans System.out.println("result of getMemoryPoolMXBeans "); List<MemoryPoolMXBean> mpmxList = ManagementFactory.getMemoryPoolMXBeans(); for (MemoryPoolMXBean pl : mpmxList) { MemoryUsage peak = pl.getPeakUsage(); System.out.println(peak.getUsed()); } // Using getClassLoadingMXBean System.out.println("result of getClassLoadingMXBean "); ClassLoadingMXBean clmxB= ManagementFactory.getClassLoadingMXBean(); System.out.println(clmxB.getTotalLoadedClassCount()); } } }
result of getThreadMXBean Current thread count:6 result of getMemoryPoolMXBeans 382208 1974088 0 0 2795952 result of getClassLoadingMXBean 344