private void monitorGc(YajswConfigurationImpl config)
{
if (config.containsKey("wrapper.java.monitor.gc"))
try
{
final MessageFormat format = new MessageFormat(config.getString("wrapper.java.monitor.gc"));
final long cycle = config.getLong("wrapper.java.monitor.gc.interval", 1)*1000;
GarbageCollectorMXBean minorGCBeanX = null;
GarbageCollectorMXBean fullGCBeanX = null;
List<GarbageCollectorMXBean> gcMBeans = ManagementFactory.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean gcBean : gcMBeans) {
if ("Copy".equals(gcBean.getName())) {
minorGCBeanX = gcBean;
} else if ("MarkSweepCompact".equals(gcBean.getName())) {
fullGCBeanX = gcBean;
} else if ("ParNew".equals(gcBean.getName())) {
minorGCBeanX = gcBean;
} else if ("ConcurrentMarkSweep".equals(gcBean.getName())) {
fullGCBeanX = gcBean;
} else {
System.err.println("Unable to classify GarbageCollectorMXBean [" + gcBean.getName() + "]");
}
}
final GarbageCollectorMXBean minorGCBean = minorGCBeanX;
final GarbageCollectorMXBean fullGCBean = fullGCBeanX;
final MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
System.err.println("monitor gc: start");
executor.execute(new Runnable()
{
private long lastMinorCollectionCount;
private long lastMinorCollectionTime;
private long lastFullCollectionCount;
private long lastFullCollectionTime;
Long usedHeap = null;
Long timeMinorGC = null;
Long timeFullGC = null;
public void run()
{
if (minorGCBean == null)
{
System.err.println("monitor gc: could not find minorGCBean -> abort monitor");
return;
}
if (fullGCBean == null)
{
System.err.println("monitor gc: could not find fullGCBean -> abort monitor");
return;
}
try
{
while (!_stopping)
{
if (minorGCBean.getCollectionCount() != lastMinorCollectionCount) {
long diffCount = minorGCBean.getCollectionCount() - lastMinorCollectionCount;
long diffTime = minorGCBean.getCollectionTime() - lastMinorCollectionTime;
if (diffCount!= 0 && diffCount != 1)
timeMinorGC = diffTime/diffCount;
else
timeMinorGC = diffTime;
usedHeap = bean.getHeapMemoryUsage().getUsed();
lastMinorCollectionCount = minorGCBean.getCollectionCount();
lastMinorCollectionTime = minorGCBean.getCollectionTime();
}
if (fullGCBean.getCollectionCount() != lastFullCollectionCount) {
long diffCount = fullGCBean.getCollectionCount() - lastFullCollectionCount;
long diffTime = fullGCBean.getCollectionTime() - lastFullCollectionTime;
if (diffCount!= 0 && diffCount != 1)
timeFullGC = diffTime/diffCount;
else
timeFullGC = diffTime;
usedHeap = bean.getHeapMemoryUsage().getUsed();
lastFullCollectionCount = fullGCBean.getCollectionCount();
lastFullCollectionTime = fullGCBean.getCollectionTime();
}
if (usedHeap != null)
{
if (timeMinorGC == null)
timeMinorGC = 0L;
if (timeFullGC == null)
timeFullGC = 0L;
System.err.println(format.format(new Object[]{usedHeap, timeMinorGC, timeFullGC}));
usedHeap = null;
timeMinorGC = null;
timeFullGC = null;
}