0 Replies Latest reply on Feb 3, 2010 9:32 AM by 843798

    Gather statistics about classloader

    843798
      I can gather statistics like
      1.No of classes loaded.
      2.Memory used by the JVM.
      3.Threads created about the JVM (default ClassLoader of the JVM).
      I need to do the same for any generic ClassLoader.

      The code I use to gather information for the JVM's ClassLoader is as follows:

      import java.io.IOException;
      import java.lang.management.ClassLoadingMXBean;
      import java.lang.management.ManagementFactory;
      import java.lang.management.OperatingSystemMXBean;
      import java.net.MalformedURLException;
      import javax.management.MBeanServer;
      import javax.management.MBeanServerConnection;
      import javax.management.remote.JMXConnector;
      import javax.management.remote.JMXConnectorFactory;
      import javax.management.remote.JMXConnectorServer;
      import javax.management.remote.JMXConnectorServerFactory;
      import javax.management.remote.JMXServiceURL;

      public class ClassLoading {

      public static void main(String args[]) throws MalformedURLException, IOException
      {
      MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
      JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
      JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
      cs.start();
      JMXServiceURL url1 = cs.getAddress();
      JMXConnector jmxc = JMXConnectorFactory.connect(url1, null);
      MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
      final ClassLoadingMXBean clmb = ManagementFactory.newPlatformMXBeanProxy(mbsc, ManagementFactory.CLASS_LOADING_MXBEAN_NAME, ClassLoadingMXBean.class);
      System.out.println("Loaded class count"+clmb.getLoadedClassCount());
      final OperatingSystemMXBean osmxb = ManagementFactory.newPlatformMXBeanProxy(mbsc, ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, OperatingSystemMXBean.class);
      System.out.println("Name"+osmxb.getName());
      }
      }