public static void main(String args[]) { MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage(); long init = heapMemoryUsage.getInit(); long used = heapMemoryUsage.getUsed(); long max = heapMemoryUsage.getMax(); long committed = heapMemoryUsage.getCommitted(); long free = max - used; int usedPercent = (int) (used / max); int freePercent = (int) (usedPercent / max); MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage(); List<GarbageCollectorMXBean> garbageCollectorMXBeans = ManagementFactory.getGarbageCollectorMXBeans(); List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans(); com.sun.management.OperatingSystemMXBean operatingSystemMXBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); long commitVirtual = operatingSystemMXBean.getCommittedVirtualMemorySize(); println(init, used, max, committed); println(commitVirtual); }
private void createRuntimeProps(MemberStateImpl memberState) { Runtime runtime = Runtime.getRuntime(); ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean(); RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean(); ClassLoadingMXBean clMxBean = ManagementFactory.getClassLoadingMXBean(); MemoryMXBean memoryMxBean = ManagementFactory.getMemoryMXBean(); MemoryUsage heapMemory = memoryMxBean.getHeapMemoryUsage(); MemoryUsage nonHeapMemory = memoryMxBean.getNonHeapMemoryUsage(); Map<String, Long> map = new HashMap<String, Long>(); map.put( "runtime.availableProcessors", Integer.valueOf(runtime.availableProcessors()).longValue()); map.put("date.startTime", runtimeMxBean.getStartTime()); map.put("seconds.upTime", runtimeMxBean.getUptime()); map.put("memory.maxMemory", runtime.maxMemory()); map.put("memory.freeMemory", runtime.freeMemory()); map.put("memory.totalMemory", runtime.totalMemory()); map.put("memory.heapMemoryMax", heapMemory.getMax()); map.put("memory.heapMemoryUsed", heapMemory.getUsed()); map.put("memory.nonHeapMemoryMax", nonHeapMemory.getMax()); map.put("memory.nonHeapMemoryUsed", nonHeapMemory.getUsed()); map.put("runtime.totalLoadedClassCount", clMxBean.getTotalLoadedClassCount()); map.put( "runtime.loadedClassCount", Integer.valueOf(clMxBean.getLoadedClassCount()).longValue()); map.put("runtime.unloadedClassCount", clMxBean.getUnloadedClassCount()); map.put("runtime.totalStartedThreadCount", threadMxBean.getTotalStartedThreadCount()); map.put("runtime.threadCount", Integer.valueOf(threadMxBean.getThreadCount()).longValue()); map.put( "runtime.peakThreadCount", Integer.valueOf(threadMxBean.getPeakThreadCount()).longValue()); map.put( "runtime.daemonThreadCount", Integer.valueOf(threadMxBean.getDaemonThreadCount()).longValue()); memberState.setRuntimeProps(map); }
/** * Callback routine called by JVM after full gc run. Has two functions: 1) sets the amount of * memory to be cleaned from the cache by the Cleaner 2) sets the CAN_ALLOC flag to false if * memory level is critical * * <p>The callback happens in a system thread, and hence not through the usual water.Boot loader * - and so any touched classes are in the wrong class loader and you end up with new classes * with uninitialized global vars. Limit to touching global vars in the Boot class. */ public void handleNotification(Notification notification, Object handback) { String notifType = notification.getType(); if (notifType.equals(MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) { // Memory used after this FullGC Boot.TIME_AT_LAST_GC = System.currentTimeMillis(); Boot.HEAP_USED_AT_LAST_GC = _allMemBean.getHeapMemoryUsage().getUsed(); Boot.kick_store_cleaner(); } }
/** * Prints the verbose GC log to System.out to list the memory usage of all memory pools as well as * the GC statistics. */ public void printVerboseGc() { System.out.println("Uptime: " + formatMillis(rmbean.getUptime())); System.out.println("Heap usage: " + mmbean.getHeapMemoryUsage()); System.out.println("Non-Heap memory usage: " + mmbean.getNonHeapMemoryUsage()); for (GarbageCollectorMXBean gc : gcmbeans) { System.out.print(" [" + gc.getName() + ": "); System.out.print("Count=" + gc.getCollectionCount()); System.out.print(" GCTime=" + formatMillis(gc.getCollectionTime())); System.out.print("]"); } System.out.println(); for (MemoryPoolMXBean p : pools) { System.out.print(" [" + p.getName() + ":"); MemoryUsage u = p.getUsage(); System.out.print(" Used=" + formatBytes(u.getUsed())); System.out.print(" Committed=" + formatBytes(u.getCommitted())); System.out.println("]"); } }
synchronized Result formatSummary() { Result result = new Result(); ProxyClient proxyClient = vmPanel.getProxyClient(); if (proxyClient.isDead()) { return null; } buf = new StringBuilder(); append("<table cellpadding=1>"); try { RuntimeMXBean rmBean = proxyClient.getRuntimeMXBean(); CompilationMXBean cmpMBean = proxyClient.getCompilationMXBean(); ThreadMXBean tmBean = proxyClient.getThreadMXBean(); MemoryMXBean memoryBean = proxyClient.getMemoryMXBean(); ClassLoadingMXBean clMBean = proxyClient.getClassLoadingMXBean(); OperatingSystemMXBean osMBean = proxyClient.getOperatingSystemMXBean(); com.sun.management.OperatingSystemMXBean sunOSMBean = proxyClient.getSunOperatingSystemMXBean(); append("<tr><td colspan=4>"); append("<center><b>" + Messages.SUMMARY_TAB_TAB_NAME + "</b></center>"); String dateTime = headerDateTimeFormat.format(System.currentTimeMillis()); append("<center>" + dateTime + "</center>"); append(newDivider); { // VM info append(newLeftTable); append(Messages.CONNECTION_NAME, vmPanel.getDisplayName()); append( Messages.VIRTUAL_MACHINE, Resources.format( Messages.SUMMARY_TAB_VM_VERSION, rmBean.getVmName(), rmBean.getVmVersion())); append(Messages.VENDOR, rmBean.getVmVendor()); append(Messages.NAME, rmBean.getName()); append(endTable); append(newRightTable); result.upTime = rmBean.getUptime(); append(Messages.UPTIME, formatTime(result.upTime)); if (sunOSMBean != null) { result.processCpuTime = sunOSMBean.getProcessCpuTime(); append(Messages.PROCESS_CPU_TIME, formatNanoTime(result.processCpuTime)); } if (cmpMBean != null) { append(Messages.JIT_COMPILER, cmpMBean.getName()); append( Messages.TOTAL_COMPILE_TIME, cmpMBean.isCompilationTimeMonitoringSupported() ? formatTime(cmpMBean.getTotalCompilationTime()) : Messages.UNAVAILABLE); } else { append(Messages.JIT_COMPILER, Messages.UNAVAILABLE); } append(endTable); } append(newDivider); { // Threads and Classes append(newLeftTable); int tlCount = tmBean.getThreadCount(); int tdCount = tmBean.getDaemonThreadCount(); int tpCount = tmBean.getPeakThreadCount(); long ttCount = tmBean.getTotalStartedThreadCount(); String[] strings1 = formatLongs( tlCount, tpCount, tdCount, ttCount); append(Messages.LIVE_THREADS, strings1[0]); append(Messages.PEAK, strings1[1]); append(Messages.DAEMON_THREADS, strings1[2]); append(Messages.TOTAL_THREADS_STARTED, strings1[3]); append(endTable); append(newRightTable); long clCount = clMBean.getLoadedClassCount(); long cuCount = clMBean.getUnloadedClassCount(); long ctCount = clMBean.getTotalLoadedClassCount(); String[] strings2 = formatLongs(clCount, cuCount, ctCount); append(Messages.CURRENT_CLASSES_LOADED, strings2[0]); append(Messages.TOTAL_CLASSES_LOADED, strings2[2]); append(Messages.TOTAL_CLASSES_UNLOADED, strings2[1]); append(null, ""); append(endTable); } append(newDivider); { // Memory MemoryUsage u = memoryBean.getHeapMemoryUsage(); append(newLeftTable); String[] strings1 = formatKByteStrings(u.getUsed(), u.getMax()); append(Messages.CURRENT_HEAP_SIZE, strings1[0]); append(Messages.MAXIMUM_HEAP_SIZE, strings1[1]); append(endTable); append(newRightTable); String[] strings2 = formatKByteStrings(u.getCommitted()); append(Messages.COMMITTED_MEMORY, strings2[0]); append( Messages.SUMMARY_TAB_PENDING_FINALIZATION_LABEL, Messages.SUMMARY_TAB_PENDING_FINALIZATION_VALUE, memoryBean.getObjectPendingFinalizationCount()); append(endTable); append(newTable); Collection<GarbageCollectorMXBean> garbageCollectors = proxyClient.getGarbageCollectorMXBeans(); for (GarbageCollectorMXBean garbageCollectorMBean : garbageCollectors) { String gcName = garbageCollectorMBean.getName(); long gcCount = garbageCollectorMBean.getCollectionCount(); long gcTime = garbageCollectorMBean.getCollectionTime(); append( Messages.GARBAGE_COLLECTOR, Resources.format( Messages.GC_INFO, gcName, gcCount, (gcTime >= 0) ? formatTime(gcTime) : Messages.UNAVAILABLE), 4); } append(endTable); } append(newDivider); { // Operating System info append(newLeftTable); String osName = osMBean.getName(); String osVersion = osMBean.getVersion(); String osArch = osMBean.getArch(); result.nCPUs = osMBean.getAvailableProcessors(); append(Messages.OPERATING_SYSTEM, osName + " " + osVersion); append(Messages.ARCHITECTURE, osArch); append(Messages.NUMBER_OF_PROCESSORS, result.nCPUs + ""); if (pathSeparator == null) { // Must use separator of remote OS, not File.pathSeparator // from this local VM. In the future, consider using // RuntimeMXBean to get the remote system property. pathSeparator = osName.startsWith("Windows ") ? ";" : ":"; } if (sunOSMBean != null) { String[] kbStrings1 = formatKByteStrings(sunOSMBean.getCommittedVirtualMemorySize()); String[] kbStrings2 = formatKByteStrings( sunOSMBean.getTotalPhysicalMemorySize(), sunOSMBean.getFreePhysicalMemorySize(), sunOSMBean.getTotalSwapSpaceSize(), sunOSMBean.getFreeSwapSpaceSize()); append(Messages.COMMITTED_VIRTUAL_MEMORY, kbStrings1[0]); append(endTable); append(newRightTable); append(Messages.TOTAL_PHYSICAL_MEMORY, kbStrings2[0]); append(Messages.FREE_PHYSICAL_MEMORY, kbStrings2[1]); append(Messages.TOTAL_SWAP_SPACE, kbStrings2[2]); append(Messages.FREE_SWAP_SPACE, kbStrings2[3]); } append(endTable); } append(newDivider); { // VM arguments and paths append(newTable); String args = ""; java.util.List<String> inputArguments = rmBean.getInputArguments(); for (String arg : inputArguments) { args += arg + " "; } append(Messages.VM_ARGUMENTS, args, 4); append(Messages.CLASS_PATH, rmBean.getClassPath(), 4); append(Messages.LIBRARY_PATH, rmBean.getLibraryPath(), 4); append( Messages.BOOT_CLASS_PATH, rmBean.isBootClassPathSupported() ? rmBean.getBootClassPath() : Messages.UNAVAILABLE, 4); append(endTable); } } catch (IOException e) { if (JConsole.isDebug()) { e.printStackTrace(); } proxyClient.markAsDead(); return null; } catch (UndeclaredThrowableException e) { if (JConsole.isDebug()) { e.printStackTrace(); } proxyClient.markAsDead(); return null; } append("</table>"); result.timeStamp = System.currentTimeMillis(); result.summary = buf.toString(); return result; }
/** Exports stats related to the JVM and runtime environment. */ public static void export() { final OperatingSystemMXBean osMbean = ManagementFactory.getOperatingSystemMXBean(); if (osMbean instanceof com.sun.management.OperatingSystemMXBean) { final com.sun.management.OperatingSystemMXBean sunOsMbean = (com.sun.management.OperatingSystemMXBean) osMbean; Stats.exportAll( ImmutableList.<Stat<? extends Number>>builder() .add( new StatImpl<Long>("system_free_physical_memory_mb") { @Override public Long read() { return sunOsMbean.getFreePhysicalMemorySize() / BYTES_PER_MB; } }) .add( new StatImpl<Long>("system_free_swap_mb") { @Override public Long read() { return sunOsMbean.getFreeSwapSpaceSize() / BYTES_PER_MB; } }) .add( Rate.of( new StatImpl<Long>("process_cpu_time_nanos") { @Override public Long read() { return sunOsMbean.getProcessCpuTime(); } }) .withName("process_cpu_cores_utilized") .withScaleFactor(SECS_PER_NANO) .build()) .build()); } if (osMbean instanceof com.sun.management.UnixOperatingSystemMXBean) { final com.sun.management.UnixOperatingSystemMXBean unixOsMbean = (com.sun.management.UnixOperatingSystemMXBean) osMbean; Stats.exportAll( ImmutableList.<Stat<? extends Number>>builder() .add( new StatImpl<Long>("process_max_fd_count") { @Override public Long read() { return unixOsMbean.getMaxFileDescriptorCount(); } }) .add( new StatImpl<Long>("process_open_fd_count") { @Override public Long read() { return unixOsMbean.getOpenFileDescriptorCount(); } }) .build()); } final Runtime runtime = Runtime.getRuntime(); final ClassLoadingMXBean classLoadingBean = ManagementFactory.getClassLoadingMXBean(); MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean(); final MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage(); final MemoryUsage nonHeapUsage = memoryBean.getNonHeapMemoryUsage(); final ThreadMXBean threads = ManagementFactory.getThreadMXBean(); final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean(); Stats.exportAll( ImmutableList.<Stat<? extends Number>>builder() .add( new StatImpl<Long>("jvm_time_ms") { @Override public Long read() { return System.currentTimeMillis(); } }) .add( new StatImpl<Integer>("jvm_available_processors") { @Override public Integer read() { return runtime.availableProcessors(); } }) .add( new StatImpl<Long>("jvm_memory_free_mb") { @Override public Long read() { return runtime.freeMemory() / BYTES_PER_MB; } }) .add( new StatImpl<Long>("jvm_memory_max_mb") { @Override public Long read() { return runtime.maxMemory() / BYTES_PER_MB; } }) .add( new StatImpl<Long>("jvm_memory_mb_total") { @Override public Long read() { return runtime.totalMemory() / BYTES_PER_MB; } }) .add( new StatImpl<Integer>("jvm_class_loaded_count") { @Override public Integer read() { return classLoadingBean.getLoadedClassCount(); } }) .add( new StatImpl<Long>("jvm_class_total_loaded_count") { @Override public Long read() { return classLoadingBean.getTotalLoadedClassCount(); } }) .add( new StatImpl<Long>("jvm_class_unloaded_count") { @Override public Long read() { return classLoadingBean.getUnloadedClassCount(); } }) .add( new StatImpl<Long>("jvm_gc_collection_time_ms") { @Override public Long read() { long collectionTimeMs = 0; for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) { collectionTimeMs += bean.getCollectionTime(); } return collectionTimeMs; } }) .add( new StatImpl<Long>("jvm_gc_collection_count") { @Override public Long read() { long collections = 0; for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) { collections += bean.getCollectionCount(); } return collections; } }) .add( new StatImpl<Long>("jvm_memory_heap_mb_used") { @Override public Long read() { return heapUsage.getUsed() / BYTES_PER_MB; } }) .add( new StatImpl<Long>("jvm_memory_heap_mb_committed") { @Override public Long read() { return heapUsage.getCommitted() / BYTES_PER_MB; } }) .add( new StatImpl<Long>("jvm_memory_heap_mb_max") { @Override public Long read() { return heapUsage.getMax() / BYTES_PER_MB; } }) .add( new StatImpl<Long>("jvm_memory_non_heap_mb_used") { @Override public Long read() { return nonHeapUsage.getUsed() / BYTES_PER_MB; } }) .add( new StatImpl<Long>("jvm_memory_non_heap_mb_committed") { @Override public Long read() { return nonHeapUsage.getCommitted() / BYTES_PER_MB; } }) .add( new StatImpl<Long>("jvm_memory_non_heap_mb_max") { @Override public Long read() { return nonHeapUsage.getMax() / BYTES_PER_MB; } }) .add( new StatImpl<Long>("jvm_uptime_secs") { @Override public Long read() { return runtimeMXBean.getUptime() / 1000; } }) .add( new StatImpl<Double>("system_load_avg") { @Override public Double read() { return osMbean.getSystemLoadAverage(); } }) .add( new StatImpl<Integer>("jvm_threads_peak") { @Override public Integer read() { return threads.getPeakThreadCount(); } }) .add( new StatImpl<Long>("jvm_threads_started") { @Override public Long read() { return threads.getTotalStartedThreadCount(); } }) .add( new StatImpl<Integer>("jvm_threads_daemon") { @Override public Integer read() { return threads.getDaemonThreadCount(); } }) .add( new StatImpl<Integer>("jvm_threads_active") { @Override public Integer read() { return threads.getThreadCount(); } }) .build()); // Export per memory pool gc time and cycle count like Ostrich // This is based on code in Bridcage: https://cgit.twitter.biz/birdcage/tree/ \ // ostrich/src/main/scala/com/twitter/ostrich/stats/StatsCollection.scala Stats.exportAll( Iterables.transform( ManagementFactory.getGarbageCollectorMXBeans(), new Function<GarbageCollectorMXBean, Stat<? extends Number>>() { @Override public Stat<? extends Number> apply(final GarbageCollectorMXBean gcMXBean) { return new StatImpl<Long>( "jvm_gc_" + Stats.normalizeName(gcMXBean.getName()) + "_collection_count") { @Override public Long read() { return gcMXBean.getCollectionCount(); } }; } })); Stats.exportAll( Iterables.transform( ManagementFactory.getGarbageCollectorMXBeans(), new Function<GarbageCollectorMXBean, Stat<? extends Number>>() { @Override public Stat<? extends Number> apply(final GarbageCollectorMXBean gcMXBean) { return new StatImpl<Long>( "jvm_gc_" + Stats.normalizeName(gcMXBean.getName()) + "_collection_time_ms") { @Override public Long read() { return gcMXBean.getCollectionTime(); } }; } })); Stats.exportString( new StatImpl<String>("jvm_input_arguments") { @Override public String read() { return runtimeMXBean.getInputArguments().toString(); } }); for (final String property : System.getProperties().stringPropertyNames()) { Stats.exportString( new StatImpl<String>("jvm_prop_" + Stats.normalizeName(property)) { @Override public String read() { return System.getProperty(property); } }); } }