package gc; import java.lang.management.ManagementFactory; import com.sun.management.OperatingSystemMXBean; import java.lang.management.ThreadMXBean; import java.lang.management.MemoryMXBean; import java.lang.management.GarbageCollectorMXBean; import java.util.*; public class GCUtils { private OperatingSystemMXBean operatingSystem; private ThreadMXBean thread; private MemoryMXBean memory; private List gcBeans; private int processorCount; private long lastCpuTime; private long lastNanoTime; private final ArrayList youngGenCollectorNames; private final ArrayList oldGenCollectorNames; public GCUtils() { operatingSystem = (OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean(); thread = ManagementFactory.getThreadMXBean(); memory = ManagementFactory.getMemoryMXBean(); gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); processorCount = operatingSystem.getAvailableProcessors(); lastCpuTime = operatingSystem.getProcessCpuTime(); lastNanoTime = System.nanoTime(); youngGenCollectorNames = new ArrayList(); youngGenCollectorNames.add("Copy"); youngGenCollectorNames.add("ParNew"); youngGenCollectorNames.add("PS Scavenge"); youngGenCollectorNames.add("Garbage collection optimized for short pausetimes Young Collector"); youngGenCollectorNames.add("Garbage collection optimized for throughput Young Collector"); youngGenCollectorNames.add("Garbage collection optimized for deterministic pausetimes Young Collector"); oldGenCollectorNames = new ArrayList(); oldGenCollectorNames.add("MarkSweepCompact"); oldGenCollectorNames.add("PS MarkSweep"); oldGenCollectorNames.add("ConcurrentMarkSweep"); oldGenCollectorNames.add("Garbage collection optimized for short pausetimes Old Collector"); oldGenCollectorNames.add("Garbage collection optimized for throughput Old Collector"); oldGenCollectorNames.add("Garbage collection optimized for deterministic pausetimes Old Collector"); } public String getUsedCpu() { long cpuTime = operatingSystem.getProcessCpuTime(); long time = System.nanoTime(); double pct = (cpuTime - lastCpuTime) / (double)(time - lastNanoTime) / processorCount; lastCpuTime = cpuTime; lastNanoTime = time; return String.format("%.2f%%", pct*100); } public long getUsedHeap() { return memory.getHeapMemoryUsage().getUsed() >> 20; } public long getCommittedHeap() { return memory.getHeapMemoryUsage().getCommitted() >> 20; } private static void printGC(GarbageCollectorMXBean gc) { String gcName = gc.getName(); long gcCnt = gc.getCollectionCount(); long gcTime = gc.getCollectionTime(); System.out.println(String.format("%s: gc count %d, gc time %d.", gcName, gcCnt, gcTime)); System.out.println(Arrays.toString(gc.getMemoryPoolNames())); } public void printYoungGC() { for (int i = 0; i < gcBeans.size(); i++) { GarbageCollectorMXBean gc = (GarbageCollectorMXBean)gcBeans.get(i); if (youngGenCollectorNames.contains(gc.getName())) { printGC(gc); } } } public void printFullGC() { for (int i = 0; i < gcBeans.size(); i++) { GarbageCollectorMXBean gc = (GarbageCollectorMXBean)gcBeans.get(i); if (oldGenCollectorNames.contains(gc.getName())) { printGC(gc); } } } public long getYoungGCCount() { long gcCount = 0L; for (int i = 0; i < gcBeans.size(); i++) { GarbageCollectorMXBean gc = (GarbageCollectorMXBean)gcBeans.get(i); if (youngGenCollectorNames.contains(gc.getName())) { gcCount += gc.getCollectionCount(); } } return gcCount; } public long getFullGCCount() { long gcCount = 0L; for (int i = 0; i < gcBeans.size(); i++) { GarbageCollectorMXBean gc = (GarbageCollectorMXBean)gcBeans.get(i); if (oldGenCollectorNames.contains(gc.getName())) { gcCount += gc.getCollectionCount(); } } return gcCount; } public long getYoungGCTime() { long gcTime = 0L; for (int i = 0; i < gcBeans.size(); i++) { GarbageCollectorMXBean gc = (GarbageCollectorMXBean)gcBeans.get(i); if (youngGenCollectorNames.contains(gc.getName())) { gcTime += gc.getCollectionTime(); } } return gcTime; } public long getFullGCTime() { long gcTime = 0L; for (int i = 0; i < gcBeans.size(); i++) { GarbageCollectorMXBean gc = (GarbageCollectorMXBean)gcBeans.get(i); if (oldGenCollectorNames.contains(gc.getName())) { gcTime += gc.getCollectionTime(); } } return gcTime; } public static void main(String[] args) { GCUtils gc = new GCUtils(); System.out.println(String.format("cpu %s.", gc.getUsedCpu())); System.out.println(String.format("Used heap %dM.", gc.getUsedHeap())); System.out.println(String.format("Committed heap %dM.", gc.getCommittedHeap())); gc.printYoungGC(); gc.printFullGC(); } }