1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
| import java.lang.management.ClassLoadingMXBean; import java.lang.management.CompilationMXBean; import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; import java.lang.management.MemoryPoolMXBean; import java.lang.management.MemoryUsage; import java.lang.management.OperatingSystemMXBean; import java.lang.management.RuntimeMXBean; import java.lang.management.ThreadMXBean; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.text.NumberFormat; import java.util.List; import java.util.Locale;
public class HeapMain {
private static NumberFormat fmtI = new DecimalFormat("###,###", new DecimalFormatSymbols(Locale.ENGLISH)); private static NumberFormat fmtD = new DecimalFormat("###,##0.000", new DecimalFormatSymbols(Locale.ENGLISH));
public static void main(String[] args) { RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean(); OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean(); ThreadMXBean threads = ManagementFactory.getThreadMXBean(); MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); MemoryUsage nonHeapMemoryUsage = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage(); ClassLoadingMXBean cl = ManagementFactory.getClassLoadingMXBean(); List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); CompilationMXBean cm = ManagementFactory.getCompilationMXBean(); List<GarbageCollectorMXBean> gcmList = ManagementFactory.getGarbageCollectorMXBeans(); System.out.printf("jvm.name (JVM名称-版本号-供应商):%s | version: %s | vendor: %s %n", runtime.getVmName(), runtime.getVmVersion(), runtime.getVmVendor()); System.out.printf("jvm.spec.name (JVM规范名称-版本号-供应商):%s | version: %s | vendor: %s %n", runtime.getSpecName(), runtime.getSpecVersion(), runtime.getSpecVendor()); System.out.printf("jvm.java.version (JVM JAVA版本):%s%n", System.getProperty("java.version")); System.out.printf("jvm.start.time (Java虚拟机的启动时间):%s%n", toDuration(runtime.getStartTime())); System.out.printf("jvm.uptime (Java虚拟机的正常运行时间):%s%n", toDuration(runtime.getUptime())); System.out.println("------------------------------------------------------------------------------------------------------"); System.out.printf("compilation.name(编译器名称):%s%n",cm.getName()); System.out.printf("compilation.total.time(编译器耗时):%d毫秒%n",cm.getTotalCompilationTime()); boolean isSupport=cm.isCompilationTimeMonitoringSupported(); if(isSupport){ System.out.println("支持即时编译器编译监控"); }else{ System.out.println("不支持即时编译器编译监控"); } System.out.printf("------------------------------------------------------------------------------------------------------"); System.out.printf("jvm.threads.total.count (总线程数(守护+非守护)):%d%n", threads.getThreadCount()); System.out.printf("jvm.threads.daemon.count (守护进程线程数):%d%n", threads.getDaemonThreadCount()); System.out.printf("jvm.threads.peak.count (峰值线程数):%d%n", threads.getPeakThreadCount()); System.out.printf("jvm.threads.total.start.count(Java虚拟机启动后创建并启动的线程总数):%d%n", threads.getTotalStartedThreadCount()); for(Long threadId : threads.getAllThreadIds()) { System.out.printf("threadId: %d | threadName: %s%n", threadId, threads.getThreadInfo(threadId).getThreadName()); } System.out.println("------------------------------------------------------------------------------------------------------"); System.out.println("------------------------------------------------------------------------------------------------------"); System.out.printf("jvm.heap.init (初始化堆内存):%s %n", bytesToMB(heapMemoryUsage.getInit())); System.out.printf("jvm.heap.used (已使用堆内存):%s %n", bytesToMB(heapMemoryUsage.getUsed())); System.out.printf("jvm.heap.committed (可使用堆内存):%s %n", bytesToMB(heapMemoryUsage.getCommitted())); System.out.printf("jvm.heap.max (最大堆内存):%s %n", bytesToMB(heapMemoryUsage.getMax())); System.out.println("------------------------------------------------------------------------------------------------------"); System.out.printf("jvm.noheap.init (初始化非堆内存):%s %n", bytesToMB(nonHeapMemoryUsage.getInit())); System.out.printf("jvm.noheap.used (已使用非堆内存):%s %n", bytesToMB(nonHeapMemoryUsage.getUsed())); System.out.printf("jvm.noheap.committed (可使用非堆内存):%s %n", bytesToMB(nonHeapMemoryUsage.getCommitted())); System.out.printf("jvm.noheap.max (最大非堆内存):%s %n", bytesToMB(nonHeapMemoryUsage.getMax())); System.out.println("------------------------------------------------------------------------------------------------------"); System.out.printf("os.name(操作系统名称-版本号):%s %s %s %n", os.getName(), "version", os.getVersion()); System.out.printf("os.arch(操作系统内核):%s%n", os.getArch()); System.out.printf("os.cores(可用的处理器数量):%s %n", os.getAvailableProcessors()); System.out.printf("os.loadAverage(系统负载平均值):%s %n", os.getSystemLoadAverage()); System.out.println("------------------------------------------------------------------------------------------------------"); System.out.printf("class.current.load.count(当前加载类数量):%s %n", cl.getLoadedClassCount()); System.out.printf("class.unload.count(未加载类数量):%s %n", cl.getUnloadedClassCount()); System.out.printf("class.total.load.count(总加载类数量):%s %n", cl.getTotalLoadedClassCount()); System.out.println("------------------------------------------------------------------------------------------------------");
for(MemoryPoolMXBean pool : pools) { final String kind = pool.getType().name(); final MemoryUsage usage = pool.getUsage(); System.out.println("内存模型: " + getKindName(kind) + ", 内存空间名称: " + getPoolName(pool.getName()) + ", jvm." + pool.getName() + ".init(初始化):" + bytesToMB(usage.getInit())); System.out.println("内存模型: " + getKindName(kind) + ", 内存空间名称: " + getPoolName(pool.getName()) + ", jvm." + pool.getName() + ".used(已使用): " + bytesToMB(usage.getUsed())); System.out.println("内存模型: " + getKindName(kind) + ", 内存空间名称: " + getPoolName(pool.getName()) + ", jvm." + pool.getName()+ ".committed(可使用):" + bytesToMB(usage.getCommitted())); System.out.println("内存模型: " + getKindName(kind) + ", 内存空间名称: " + getPoolName(pool.getName()) + ", jvm." + pool.getName() + ".max(最大):" + bytesToMB(usage.getMax())); System.out.println("------------------------------------------------------------------------------------------------------"); } } protected static String getKindName(String kind) { if("NON_HEAP".equals(kind)) { return "NON_HEAP(非堆内存)"; }else { return "HEAP(堆内存)"; } } protected static String getPoolName(String poolName) { switch (poolName) { case "Code Cache": return poolName +"(代码缓存区)"; case "Metaspace": return poolName +"(元空间)"; case "Compressed Class Space": return poolName +"(类指针压缩空间)"; case "PS Eden Space": return poolName +"(伊甸园区)"; case "PS Survivor Space": return poolName +"(幸存者区)"; case "PS Old Gen": return poolName +"(老年代)"; default: return poolName; } } protected static String bytesToMB(long bytes) { return fmtI.format((long)(bytes / 1024 / 1024)) + " MB"; } protected static String printSizeInKb(double size) { return fmtI.format((long) (size / 1024)) + " kbytes"; }
protected static String toDuration(double uptime) { uptime /= 1000; if (uptime < 60) { return fmtD.format(uptime) + " seconds"; } uptime /= 60; if (uptime < 60) { long minutes = (long) uptime; String s = fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute"); return s; } uptime /= 60; if (uptime < 24) { long hours = (long) uptime; long minutes = (long) ((uptime - hours) * 60); String s = fmtI.format(hours) + (hours > 1 ? " hours" : " hour"); if (minutes != 0) { s += " " + fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute"); } return s; } uptime /= 24; long days = (long) uptime; long hours = (long) ((uptime - days) * 24); String s = fmtI.format(days) + (days > 1 ? " days" : " day"); if (hours != 0) { s += " " + fmtI.format(hours) + (hours > 1 ? " hours" : " hour"); } return s; }
}
|