0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A *
0N/A * Redistribution and use in source and binary forms, with or without
0N/A * modification, are permitted provided that the following conditions
0N/A * are met:
0N/A *
0N/A * - Redistributions of source code must retain the above copyright
0N/A * notice, this list of conditions and the following disclaimer.
0N/A *
0N/A * - Redistributions in binary form must reproduce the above copyright
0N/A * notice, this list of conditions and the following disclaimer in the
0N/A * documentation and/or other materials provided with the distribution.
0N/A *
2362N/A * - Neither the name of Oracle nor the names of its
0N/A * contributors may be used to endorse or promote products derived
0N/A * from this software without specific prior written permission.
0N/A *
0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
0N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0N/A */
0N/A
0N/A/*
4378N/A * This source code is provided to illustrate the usage of a given feature
4378N/A * or technique and has been deliberately simplified. Additional steps
4378N/A * required for a production-quality application, such as security checks,
4378N/A * input validation and proper error handling, might not be present in
4378N/A * this sample code.
4378N/A */
4378N/A
4378N/A
4378N/A/*
0N/A */
0N/A
0N/Aimport static java.lang.management.ManagementFactory.*;
0N/Aimport java.lang.management.*;
0N/Aimport javax.management.*;
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/A/**
0N/A * Example of using the java.lang.management API to monitor
0N/A * the memory usage and garbage collection statistics.
0N/A *
0N/A * @author Mandy Chung
0N/A */
0N/Apublic class PrintGCStat {
0N/A private RuntimeMXBean rmbean;
0N/A private MemoryMXBean mmbean;
0N/A private List<MemoryPoolMXBean> pools;
0N/A private List<GarbageCollectorMXBean> gcmbeans;
0N/A
0N/A /**
0N/A * Constructs a PrintGCStat object to monitor a remote JVM.
0N/A */
0N/A public PrintGCStat(MBeanServerConnection server) throws IOException {
0N/A // Create the platform mxbean proxies
0N/A this.rmbean = newPlatformMXBeanProxy(server,
0N/A RUNTIME_MXBEAN_NAME,
0N/A RuntimeMXBean.class);
0N/A this.mmbean = newPlatformMXBeanProxy(server,
0N/A MEMORY_MXBEAN_NAME,
0N/A MemoryMXBean.class);
0N/A ObjectName poolName = null;
0N/A ObjectName gcName = null;
0N/A try {
0N/A poolName = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE+",*");
0N/A gcName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE+",*");
0N/A } catch (MalformedObjectNameException e) {
0N/A // should not reach here
0N/A assert(false);
0N/A }
0N/A
4123N/A Set<ObjectName> mbeans = server.queryNames(poolName, null);
0N/A if (mbeans != null) {
0N/A pools = new ArrayList<MemoryPoolMXBean>();
4123N/A for (ObjectName objName : mbeans) {
0N/A MemoryPoolMXBean p =
0N/A newPlatformMXBeanProxy(server,
0N/A objName.getCanonicalName(),
0N/A MemoryPoolMXBean.class);
0N/A pools.add(p);
0N/A }
0N/A }
0N/A
0N/A mbeans = server.queryNames(gcName, null);
0N/A if (mbeans != null) {
0N/A gcmbeans = new ArrayList<GarbageCollectorMXBean>();
4123N/A for (ObjectName objName : mbeans) {
0N/A GarbageCollectorMXBean gc =
0N/A newPlatformMXBeanProxy(server,
0N/A objName.getCanonicalName(),
0N/A GarbageCollectorMXBean.class);
0N/A gcmbeans.add(gc);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Constructs a PrintGCStat object to monitor the local JVM.
0N/A */
0N/A public PrintGCStat() {
0N/A // Obtain the platform mxbean instances for the running JVM.
0N/A this.rmbean = getRuntimeMXBean();
0N/A this.mmbean = getMemoryMXBean();
0N/A this.pools = getMemoryPoolMXBeans();
0N/A this.gcmbeans = getGarbageCollectorMXBeans();
0N/A }
0N/A
0N/A /**
0N/A * Prints the verbose GC log to System.out to list the memory usage
0N/A * of all memory pools as well as the GC statistics.
0N/A */
0N/A public void printVerboseGc() {
4123N/A System.out.println("Uptime: " + formatMillis(rmbean.getUptime()));
4123N/A System.out.println("Heap usage: " + mmbean.getHeapMemoryUsage());
4123N/A System.out.println("Non-Heap memory usage: " + mmbean.getNonHeapMemoryUsage());
0N/A for (GarbageCollectorMXBean gc : gcmbeans) {
0N/A System.out.print(" [" + gc.getName() + ": ");
0N/A System.out.print("Count=" + gc.getCollectionCount());
0N/A System.out.print(" GCTime=" + formatMillis(gc.getCollectionTime()));
0N/A System.out.print("]");
0N/A }
0N/A System.out.println();
0N/A for (MemoryPoolMXBean p : pools) {
0N/A System.out.print(" [" + p.getName() + ":");
0N/A MemoryUsage u = p.getUsage();
0N/A System.out.print(" Used=" + formatBytes(u.getUsed()));
0N/A System.out.print(" Committed=" + formatBytes(u.getCommitted()));
0N/A System.out.println("]");
0N/A }
0N/A }
0N/A
0N/A private String formatMillis(long ms) {
0N/A return String.format("%.4fsec", ms / (double) 1000);
0N/A }
0N/A private String formatBytes(long bytes) {
0N/A long kb = bytes;
0N/A if (bytes > 0) {
0N/A kb = bytes / 1024;
0N/A }
0N/A return kb + "K";
0N/A }
0N/A}