Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/ClassLoadingMXBean/LoadCounts.java
38828 views
/*1* Copyright (c) 2003, 2013, 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 453053826* @summary Basic unit test of ClassLoadingMXBean.getLoadedClassCount()27* ClassLoadingMXBean.getTotalLoadedClassCount()28* ClassLoadingMXBean.getUnloadedClassCount()29* @author Alexei Guibadoulline30* @run main/othervm LoadCounts31*/3233import java.lang.management.*;3435public class LoadCounts {36private static ClassLoadingMXBean mbean37= ManagementFactory.getClassLoadingMXBean();3839public static void main(String argv[]) throws Exception {40// Get current count41int classesNowPrev = mbean.getLoadedClassCount();42long classesTotalPrev = mbean.getTotalLoadedClassCount();4344System.out.println("Loading 4 classes with the system class loader");4546new SimpleOne();47new SimpleTwo();48new Chain();4950int classesNow = mbean.getLoadedClassCount();51long classesTotal = mbean.getTotalLoadedClassCount();5253if (classesNow > classesTotal)54throw new RuntimeException("getLoadedClassCount() > "55+ "getTotalLoadedClassCount()");5657if (classesNowPrev + 4 > classesNow)58throw new RuntimeException("Number of loaded classes is "59+ "expected to be at least "60+ (classesNowPrev + 4) + ", but "61+ "MBean.getLoadedClassCount() returned "62+ classesNow);63if (classesTotalPrev + 4 > classesTotal)64throw new RuntimeException("Total number of loaded classes is "65+ "expected to be at least "66+ (classesTotalPrev + 4) + ", but "67+ "MBean.getTotalLoadedClassCount() "68+ "returned " + classesTotal);6970System.out.println("Creating new class loader instances");7172LeftHand leftHand = new LeftHand();73RightHand rightHand = new RightHand();74LoaderForTwoInstances ins1 = new LoaderForTwoInstances();75LoaderForTwoInstances ins2 = new LoaderForTwoInstances();7677// Load different type of classes with different78// initiating classloaders but the same defining class loader.79System.out.println("Loading 2 class instances; each by " +80"2 initiating class loaders.");8182classesNowPrev = mbean.getLoadedClassCount();83classesTotalPrev = mbean.getTotalLoadedClassCount();84try {85Class.forName("Body", true, leftHand);86Class.forName("Body", true, rightHand);87Class.forName("TheSameClass", true, ins1);88Class.forName("TheSameClass", true, ins2);89} catch (ClassNotFoundException e) {90System.out.println("Unexpected excetion " + e);91e.printStackTrace(System.out);92throw new RuntimeException();93}94classesNow = mbean.getLoadedClassCount();95classesTotal = mbean.getTotalLoadedClassCount();9697// Expected 2 classes got loaded since they are loaded by98// same defining class loader99if (classesNowPrev + 2 > classesNow)100throw new RuntimeException("Number of loaded classes is "101+ "expected to be at least "102+ (classesNowPrev + 4) + ", but "103+ "MBean.getLoadedClassCount() returned "104+ classesNow);105if (classesTotalPrev + 2 > classesTotal)106throw new RuntimeException("Total number of loaded classes is "107+ "expected to be at least "108+ (classesTotalPrev + 4) + ", but "109+ "MBean.getTotalLoadedClassCount() "110+ "returned " + classesTotal);111112System.out.println("Test passed.");113}114}115116class SimpleOne {}117class SimpleTwo {}118119class Chain {120Slave slave = new Slave();121}122class Slave {}123124class LeftHand extends ClassLoader {125public LeftHand() {126super(LeftHand.class.getClassLoader());127}128}129class RightHand extends ClassLoader {130public RightHand() {131super(RightHand.class.getClassLoader());132}133}134class Body {}135136class LoaderForTwoInstances extends ClassLoader {137public LoaderForTwoInstances() {138super(LoaderForTwoInstances.class.getClassLoader());139}140}141class TheSameClass {}142143144