zoukankan      html  css  js  c++  java
  • 对jvm进行gc的时间、数量、jvm停顿时间的监控

    在jdk中一个类可以获得gc的信息:

        public static void main(String[] args) {
            List<GarbageCollectorMXBean> garbageCollectorMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
            for (GarbageCollectorMXBean garbageCollectorMXBean : garbageCollectorMXBeans) {
                System.out.println(garbageCollectorMXBean.getName() + "#  time: " + garbageCollectorMXBean.getCollectionTime() + ", count: " + garbageCollectorMXBean.getCollectionCount());
            }
        }
    

    在hadoop-common.jar中,有个JvmPauseMonitor$Monitor类,它能监控jvm暂停时间:

    private class Monitor implements Runnable {
        @Override
        public void run() {
          StopWatch sw = new StopWatch();
          Map<String, GcTimes> gcTimesBeforeSleep = getGcTimes(); // 用ManagementFactory.getGarbageCollectorMXBeans()收集得到 name:GcTime的信息
          while (shouldRun) {
            sw.reset().start();
            try {
              Thread.sleep(SLEEP_INTERVAL_MS);
            } catch (InterruptedException ie) {
              return;
            }
            long extraSleepTime = sw.now(TimeUnit.MILLISECONDS) - SLEEP_INTERVAL_MS; // 关键在这里,减去自己sleep的时间,大约等于系统暂停的时间(因为还有run方法执行其他代码的时间)
            Map<String, GcTimes> gcTimesAfterSleep = getGcTimes();
    
            if (extraSleepTime > warnThresholdMs) { // 达到warn的阈值则打印警告
              ++numGcWarnThresholdExceeded;
              LOG.warn(formatMessage(
                  extraSleepTime, gcTimesAfterSleep, gcTimesBeforeSleep));
            } else if (extraSleepTime > infoThresholdMs) {
              ++numGcInfoThresholdExceeded;
              LOG.info(formatMessage(
                  extraSleepTime, gcTimesAfterSleep, gcTimesBeforeSleep));
            }
            totalGcExtraSleepTime += extraSleepTime; // 记录jvm暂停的时间(大约)
            gcTimesBeforeSleep = gcTimesAfterSleep;
          }
        }
      }
    
  • 相关阅读:
    nodejs windows下安装运行
    第一篇博客
    vc 动态链接库编程2
    vc 动态链接库编程
    原生js实现图片在线预览
    玩转 css3
    CSS Hack整理
    PHP stdClass Object转array
    aptana studio3 汉化方法
    玩转 css3 续
  • 原文地址:https://www.cnblogs.com/lanhj/p/7493603.html
Copyright © 2011-2022 走看看