是否有使用MBean的日志jvm GC活动?

时间:2012-04-12 09:45:58

标签: java jvm

是否有使用MBean的日志jvm GC活动,cpu使用或其他jvm信息?如果是这样,怎么样?

1 个答案:

答案 0 :(得分:1)

    ClassLoadingMXBean clMxBean = ManagementFactory.getClassLoadingMXBean();

    System.out.println("loadedClassCount:" + clMxBean.getLoadedClassCount());
    System.out.println("totalLoadedClassCount:" + clMxBean.getTotalLoadedClassCount());
    System.out.println("unloadedClassCount:" + clMxBean.getUnloadedClassCount());

    List<GarbageCollectorMXBean> gcMxBeans = ManagementFactory.getGarbageCollectorMXBeans();
    for(GarbageCollectorMXBean gcMxBean : gcMxBeans)
    {
        System.out.println("\ngcMxBean: " + gcMxBean.getName() +
                 ", collection count:" + gcMxBean.getCollectionCount() +
                  ", Collection time:" + gcMxBean.getCollectionTime() + 
                ", Memory Pool Names:" + Arrays.deepToString(gcMxBean.getMemoryPoolNames())  );
    }

    OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();
    double systemLoadAverageInLast1Minute = osMxBean.getSystemLoadAverage();
    int availableProcessors = osMxBean.getAvailableProcessors();
    System.out.println("\nlast 1 minute load average over all CPUs: " + systemLoadAverageInLast1Minute);
    System.out.println("last 1 minute load average per CPU: " + (systemLoadAverageInLast1Minute/availableProcessors) );