Path: blob/master/test/jdk/com/sun/management/GarbageCollectorMXBean/LastGCInfo.java
41155 views
/*1* Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 498230126* @summary Sanity Test for GarbageCollectorMXBean.getLastGcInfo().27* @author Mandy Chung28*29* @run main/othervm -XX:-ExplicitGCInvokesConcurrent LastGCInfo30*/31// Passing "-XX:-ExplicitGCInvokesConcurrent" to force System.gc()32// run on foreground when CMS is used and prevent situations when "GcInfo"33// is missing even though System.gc() was successfuly processed.3435import java.lang.management.ManagementFactory;36import java.lang.management.MemoryUsage;37import java.lang.management.MemoryPoolMXBean;38import java.util.*;39import com.sun.management.GcInfo;40import com.sun.management.GarbageCollectorMXBean;4142public class LastGCInfo {43public static void main(String[] argv) throws Exception {44boolean hasGcInfo = false;4546System.gc();47List mgrs = ManagementFactory.getGarbageCollectorMXBeans();48for (ListIterator iter = mgrs.listIterator(); iter.hasNext(); ) {49Object mgr = iter.next();50if (mgr instanceof GarbageCollectorMXBean) {51GarbageCollectorMXBean gc = (GarbageCollectorMXBean) mgr;52GcInfo info = gc.getLastGcInfo();53if (info != null) {54checkGcInfo(gc.getName(), info);55hasGcInfo = true;56}57}58}5960if (! hasGcInfo) {61throw new RuntimeException("No GcInfo returned");62}63System.out.println("Test passed.");64}6566private static void checkGcInfo(String name, GcInfo info) throws Exception {67System.out.println("GC statistic for : " + name);68System.out.print("GC #" + info.getId());69System.out.print(" start:" + info.getStartTime());70System.out.print(" end:" + info.getEndTime());71System.out.println(" (" + info.getDuration() + "ms)");72Map usage = info.getMemoryUsageBeforeGc();7374List pnames = new ArrayList();75for (Iterator iter = usage.entrySet().iterator(); iter.hasNext(); ) {76Map.Entry entry = (Map.Entry) iter.next();77String poolname = (String) entry.getKey();78pnames.add(poolname);79MemoryUsage busage = (MemoryUsage) entry.getValue();80MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname);81if (ausage == null) {82throw new RuntimeException("After Gc Memory does not exist" +83" for " + poolname);84}85System.out.println("Usage for pool " + poolname);86System.out.println(" Before GC: " + busage);87System.out.println(" After GC: " + ausage);88}8990// check if memory usage for all memory pools are returned91List pools = ManagementFactory.getMemoryPoolMXBeans();92for (Iterator iter = pools.iterator(); iter.hasNext(); ) {93MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();94if (!pnames.contains(p.getName())) {95throw new RuntimeException("GcInfo does not contain " +96"memory usage for pool " + p.getName());97}98}99}100}101102103