Skip to content

Instantly share code, notes, and snippets.

@mikroskeem
Forked from aikar/GCMonitorTask.java
Created August 2, 2018 23:39
Show Gist options
  • Save mikroskeem/d3ffd07654c0047271c4dcd0b50e2d22 to your computer and use it in GitHub Desktop.
Save mikroskeem/d3ffd07654c0047271c4dcd0b50e2d22 to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2017. Starlis LLC / dba Empire Minecraft
*
* This source code is proprietary software and must not be redistributed without Starlis LLC's approval
*
*/
package com.empireminecraft.systems.tasks;
import com.empireminecraft.util.Log;
import com.empireminecraft.util.MovingAverage;
import com.empireminecraft.util.TimeUtil;
import com.sun.management.GarbageCollectorMXBean;
import com.sun.management.GcInfo;
import javax.management.MBeanServer;
import java.lang.management.ManagementFactory;
@SuppressWarnings("WeakerAccess")
public class GCMonitorTask implements Runnable {
private static volatile GarbageCollectorMXBean youngBean;
private static volatile GarbageCollectorMXBean oldBean;
public static long lastYoung = 0;
public static long lastOld = 0;
public static long oldCount = 0;
public static MovingAverage youngInterval = new MovingAverage(10, TimeUtil.SECOND.inMilli(30));
public static MovingAverage youngDuration = new MovingAverage(10, 50);
static {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
try {
String youngId = "java.lang:type=GarbageCollector,name=G1 Young Generation";
youngBean = ManagementFactory.newPlatformMXBeanProxy(server, youngId, GarbageCollectorMXBean.class);
if (youngBean == null) {
Log.error("Could not find YoungGen Bean");
} else {
GcInfo lastGcInfo = youngBean.getLastGcInfo();
if (lastGcInfo != null) {
lastYoung = lastGcInfo.getStartTime();
} else {
lastYoung = System.currentTimeMillis();
}
}
String oldId = "java.lang:type=GarbageCollector,name=G1 Old Generation";
oldBean = ManagementFactory.newPlatformMXBeanProxy(server, oldId, GarbageCollectorMXBean.class);
if (oldBean == null) {
Log.error("Could not find OldGen Bean");
} else {
oldCount = oldBean.getCollectionCount();
GcInfo lastGcInfo = oldBean.getLastGcInfo();
if (lastGcInfo != null) {
lastOld = lastGcInfo.getStartTime();
} else {
lastOld = 0;
}
}
} catch (Exception e) {
Log.exception(e);
}
}
@Override
public void run() {
processYoung();
processOld();
}
private static synchronized void processOld() {
if (oldBean == null) {
return;
}
GcInfo last = oldBean.getLastGcInfo();
if (last == null) {
return;
}
oldCount = oldBean.getCollectionCount();
lastOld = last.getStartTime();
}
private static synchronized void processYoung() {
if (youngBean == null) {
return;
}
GcInfo last = youngBean.getLastGcInfo();
if (last == null) {
return;
}
long startTime = last.getStartTime();
long endTime = last.getEndTime();
if (startTime == endTime) {
return;
}
if (lastYoung == startTime) {
return;
}
long diff = startTime - lastYoung;
long duration = endTime - startTime;
lastYoung = startTime;
youngInterval.add(diff);
youngDuration.add(duration);
}
}
/*
* Copyright (c) 2017. Starlis LLC / dba Empire Minecraft
*
* This source code is proprietary software and must not be redistributed without Starlis LLC's approval
*
*/
package com.empireminecraft.util;
public class MovingAverage {
private final double exp;
private double avg;
public MovingAverage(int size, double initial) {
this.avg = initial;
this.exp = (size-1D)/size;
}
public void add(double x) {
avg = (avg * exp) + (x * (1 - exp));
}
public double getAverage() {
return avg;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment