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;
          }
        }
      }
    
  • 相关阅读:
    poj 2100 尺取法 一个数字拆成连续数字平方和
    poj 1011 dfs+剪枝
    CF-242-C bfs+stl
    hdu 1297 递推
    poj 2104 划分树模板
    poj 3842 全排列+筛素数+暴力
    hdu 1421 经典dp
    hdu 1069 最长上升子序列变形
    hdu 3496 二维费用的01背包
    nyoj 16 最长上升子序列变形
  • 原文地址:https://www.cnblogs.com/lanhj/p/7493603.html
Copyright © 2011-2022 走看看