在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;
}
}
}