Path: blob/master/test/jdk/java/lang/management/ClassLoadingMXBean/LoadCounts.java
41154 views
/*1* Copyright (c) 2003, 2020, 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*31* @run main/othervm LoadCounts32*/3334import java.lang.management.*;3536public class LoadCounts {37private static ClassLoadingMXBean mbean38= ManagementFactory.getClassLoadingMXBean();3940public static void main(String argv[]) throws Exception {41// Get current count42int classesNowPrev = mbean.getLoadedClassCount();43long classesTotalPrev = mbean.getTotalLoadedClassCount();4445System.out.println("Loading 4 classes with the system class loader");4647new SimpleOne();48new SimpleTwo();49new Chain();5051int classesNow = mbean.getLoadedClassCount();52long classesTotal = mbean.getTotalLoadedClassCount();5354if (classesNow > classesTotal)55throw new RuntimeException("getLoadedClassCount() > "56+ "getTotalLoadedClassCount()");5758if (classesNowPrev + 4 > classesNow)59throw new RuntimeException("Number of loaded classes is "60+ "expected to be at least "61+ (classesNowPrev + 4) + ", but "62+ "MBean.getLoadedClassCount() returned "63+ classesNow);64if (classesTotalPrev + 4 > classesTotal)65throw new RuntimeException("Total number of loaded classes is "66+ "expected to be at least "67+ (classesTotalPrev + 4) + ", but "68+ "MBean.getTotalLoadedClassCount() "69+ "returned " + classesTotal);7071System.out.println("Creating new class loader instances");7273LeftHand leftHand = new LeftHand();74RightHand rightHand = new RightHand();75LoaderForTwoInstances ins1 = new LoaderForTwoInstances();76LoaderForTwoInstances ins2 = new LoaderForTwoInstances();7778// Load different type of classes with different79// initiating classloaders but the same defining class loader.80System.out.println("Loading 2 class instances; each by " +81"2 initiating class loaders.");8283classesNowPrev = mbean.getLoadedClassCount();84classesTotalPrev = mbean.getTotalLoadedClassCount();85try {86Class.forName("Body", true, leftHand);87Class.forName("Body", true, rightHand);88Class.forName("TheSameClass", true, ins1);89Class.forName("TheSameClass", true, ins2);90} catch (ClassNotFoundException e) {91System.out.println("Unexpected excetion " + e);92e.printStackTrace(System.out);93throw new RuntimeException();94}95classesNow = mbean.getLoadedClassCount();96classesTotal = mbean.getTotalLoadedClassCount();9798// Expected 2 classes got loaded since they are loaded by99// same defining class loader100if (classesNowPrev + 2 > classesNow)101throw new RuntimeException("Number of loaded classes is "102+ "expected to be at least "103+ (classesNowPrev + 4) + ", but "104+ "MBean.getLoadedClassCount() returned "105+ classesNow);106if (classesTotalPrev + 2 > classesTotal)107throw new RuntimeException("Total number of loaded classes is "108+ "expected to be at least "109+ (classesTotalPrev + 4) + ", but "110+ "MBean.getTotalLoadedClassCount() "111+ "returned " + classesTotal);112113System.out.println("Test passed.");114}115}116117class SimpleOne {}118class SimpleTwo {}119120class Chain {121Worker worker = new Worker();122}123class Worker {}124125class LeftHand extends ClassLoader {126public LeftHand() {127super(LeftHand.class.getClassLoader());128}129}130class RightHand extends ClassLoader {131public RightHand() {132super(RightHand.class.getClassLoader());133}134}135class Body {}136137class LoaderForTwoInstances extends ClassLoader {138public LoaderForTwoInstances() {139super(LoaderForTwoInstances.class.getClassLoader());140}141}142class TheSameClass {}143144145