public Stats getStats() {
RuntimeMXBean runmxbean = ManagementFactory.getRuntimeMXBean();
MemoryMXBean memmxbean = ManagementFactory.getMemoryMXBean();
MemoryUsage memUsage = memmxbean.getHeapMemoryUsage();
CountStatisticImpl upTime;
BoundedRangeStatisticImpl heapSize;
if (stats == null) {
stats = new JVMStatsImpl();
// setup UpTime CountStatistic
upTime = stats.getUpTimeImpl();
upTime.setStartTime(runmxbean.getStartTime());
upTime.setCount(runmxbean.getUptime());
// setup Heap BoundedRangeStatistic
heapSize = stats.getHeapSizeImpl();
heapSize.setStartTime(runmxbean.getStartTime());
heapSize.setBounds(0, memUsage.getMax());
heapSize.setCurrent(memUsage.getUsed());
heapSize.setLowWaterMark(memUsage.getUsed());
heapSize.setHighWaterMark(memUsage.getUsed());
} else {
// update UpTime CountStatistic
upTime = stats.getUpTimeImpl();
upTime.setCount(runmxbean.getUptime());
// update Heap BoundedRangeStatistic
heapSize = stats.getHeapSizeImpl();
heapSize.setBounds(0, memUsage.getMax());
heapSize.setCurrent(memUsage.getUsed());
}
long now = upTime.getStartTime() + upTime.getCount();
upTime.setLastSampleTime(now);
heapSize.setLastSampleTime(now);
return stats;
}