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;
          }
        }
      }
    
  • 相关阅读:
    Odoo Entypo Regular Icon List
    Ubuntu 循环遍历当前目录下所有文本文件中的字符
    FairyGUI学习
    FairyGUI和NGUI对比
    热更新有多重要?游戏代码热更新杂谈
    收藏的链接
    Vuforia AR实战教程
    BleedTree动画混合树
    Unity3d导出安卓版本
    Unity+高通Vuforia SDK——AR
  • 原文地址:https://www.cnblogs.com/lanhj/p/7493603.html
Copyright © 2011-2022 走看看