Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/management/VerboseGC/PrintGCStat.java
38829 views
/*1* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/383940/*41*/4243import static java.lang.management.ManagementFactory.*;44import java.lang.management.*;45import javax.management.*;46import java.io.*;47import java.util.*;4849/**50* Example of using the java.lang.management API to monitor51* the memory usage and garbage collection statistics.52*53* @author Mandy Chung54*/55public class PrintGCStat {56private RuntimeMXBean rmbean;57private MemoryMXBean mmbean;58private List<MemoryPoolMXBean> pools;59private List<GarbageCollectorMXBean> gcmbeans;6061/**62* Constructs a PrintGCStat object to monitor a remote JVM.63*/64public PrintGCStat(MBeanServerConnection server) throws IOException {65// Create the platform mxbean proxies66this.rmbean = newPlatformMXBeanProxy(server,67RUNTIME_MXBEAN_NAME,68RuntimeMXBean.class);69this.mmbean = newPlatformMXBeanProxy(server,70MEMORY_MXBEAN_NAME,71MemoryMXBean.class);72ObjectName poolName = null;73ObjectName gcName = null;74try {75poolName = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE+",*");76gcName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE+",*");77} catch (MalformedObjectNameException e) {78// should not reach here79assert(false);80}8182Set<ObjectName> mbeans = server.queryNames(poolName, null);83if (mbeans != null) {84pools = new ArrayList<MemoryPoolMXBean>();85for (ObjectName objName : mbeans) {86MemoryPoolMXBean p =87newPlatformMXBeanProxy(server,88objName.getCanonicalName(),89MemoryPoolMXBean.class);90pools.add(p);91}92}9394mbeans = server.queryNames(gcName, null);95if (mbeans != null) {96gcmbeans = new ArrayList<GarbageCollectorMXBean>();97for (ObjectName objName : mbeans) {98GarbageCollectorMXBean gc =99newPlatformMXBeanProxy(server,100objName.getCanonicalName(),101GarbageCollectorMXBean.class);102gcmbeans.add(gc);103}104}105}106107/**108* Constructs a PrintGCStat object to monitor the local JVM.109*/110public PrintGCStat() {111// Obtain the platform mxbean instances for the running JVM.112this.rmbean = getRuntimeMXBean();113this.mmbean = getMemoryMXBean();114this.pools = getMemoryPoolMXBeans();115this.gcmbeans = getGarbageCollectorMXBeans();116}117118/**119* Prints the verbose GC log to System.out to list the memory usage120* of all memory pools as well as the GC statistics.121*/122public void printVerboseGc() {123System.out.println("Uptime: " + formatMillis(rmbean.getUptime()));124System.out.println("Heap usage: " + mmbean.getHeapMemoryUsage());125System.out.println("Non-Heap memory usage: " + mmbean.getNonHeapMemoryUsage());126for (GarbageCollectorMXBean gc : gcmbeans) {127System.out.print(" [" + gc.getName() + ": ");128System.out.print("Count=" + gc.getCollectionCount());129System.out.print(" GCTime=" + formatMillis(gc.getCollectionTime()));130System.out.print("]");131}132System.out.println();133for (MemoryPoolMXBean p : pools) {134System.out.print(" [" + p.getName() + ":");135MemoryUsage u = p.getUsage();136System.out.print(" Used=" + formatBytes(u.getUsed()));137System.out.print(" Committed=" + formatBytes(u.getCommitted()));138System.out.println("]");139}140}141142private String formatMillis(long ms) {143return String.format("%.4fsec", ms / (double) 1000);144}145private String formatBytes(long bytes) {146long kb = bytes;147if (bytes > 0) {148kb = bytes / 1024;149}150return kb + "K";151}152}153154155