Skip to content

Commit

Permalink
Tracks memory pool stats (eden, survivor, tenured, etc.)
Browse files Browse the repository at this point in the history
This, along with #18 closes #17
  • Loading branch information
Benjamin Darfler committed Jul 2, 2015
1 parent 0f15862 commit 6e7e24f
Showing 1 changed file with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ public class MemoryProfiler extends Profiler {
private List<GarbageCollectorMXBean> gcMXBeans;
private HashMap<GarbageCollectorMXBean, AtomicLong> gcTimes = new HashMap<>();
private ClassLoadingMXBean classLoadingMXBean;
private List<MemoryPoolMXBean> memoryPoolMXBeans;

public MemoryProfiler(Reporter reporter, Arguments arguments) {
super(reporter, arguments);
memoryMXBean = ManagementFactory.getMemoryMXBean();
gcMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
classLoadingMXBean = ManagementFactory.getClassLoadingMXBean();
memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();

for (GarbageCollectorMXBean b : gcMXBeans) gcTimes.put(b, new AtomicLong());
}
Expand Down Expand Up @@ -67,8 +69,8 @@ private void recordStats() {
MemoryUsage nonHeap = memoryMXBean.getNonHeapMemoryUsage();

recordGaugeValue("pending-finalization-count", finalizationPendingCount);
recordMemoryUsage("heap", heap);
recordMemoryUsage("nonheap", nonHeap);
recordMemoryUsage("heap.total", heap);
recordMemoryUsage("nonheap.total", nonHeap);

for (GarbageCollectorMXBean gcMXBean : gcMXBeans) {
recordGaugeValue("gc." + gcMXBean.getName() + ".count", gcMXBean.getCollectionCount());
Expand All @@ -90,6 +92,15 @@ private void recordStats() {
recordGaugeValue("loaded-class-count", loadedClassCount);
recordGaugeValue("total-loaded-class-count", totalLoadedClassCount);
recordGaugeValue("unloaded-class-count", unloadedClassCount);

for (MemoryPoolMXBean memoryPoolMXBean: memoryPoolMXBeans) {
String type = poolTypeToMetricName(memoryPoolMXBean.getType());
String name = poolNameToMetricName(memoryPoolMXBean.getName());
String prefix = type + '.' + name;
MemoryUsage usage = memoryPoolMXBean.getUsage();

recordMemoryUsage(prefix, usage);
}
}

/**
Expand All @@ -104,4 +115,31 @@ private void recordMemoryUsage(String prefix, MemoryUsage memory) {
recordGaugeValue(prefix + ".committed", memory.getCommitted());
recordGaugeValue(prefix + ".max", memory.getMax());
}

/**
* Formats a MemoryType into a valid metric name
*
* @param memoryType a MemoryType
* @return a valid metric name
*/
private String poolTypeToMetricName(MemoryType memoryType) {
switch (memoryType) {
case HEAP:
return "heap";
case NON_HEAP:
return "nonheap";
default:
return "unknown";
}
}

/**
* Formats a pool name into a valid metric name
*
* @param poolName a pool name
* @return a valid metric name
*/
private String poolNameToMetricName(String poolName) {
return poolName.toLowerCase().replaceAll("\\s+", "-");
}
}

0 comments on commit 6e7e24f

Please sign in to comment.