Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/LoadUnloadGC/LoadUnloadGC.java
41159 views
/*1* Copyright (c) 2002, 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* @key stress26*27* @summary converted from VM Testbase gc/gctests/LoadUnloadGC.28* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, monitoring]29* VM Testbase readme:30* In this test a 1000 classes are loaded and unloaded in a loop.31* Class0 gets loaded which results in Class1 getting loaded and so on all32* the way uptill class1000. The classes should be unloaded whenever a33* garbage collection takes place because their classloader is made unreachable34* at the end of the each loop iteration. The loop is repeated 1000 times.35*36* @requires vm.opt.final.ClassUnloading37* @library /vmTestbase38* /test/lib39* @build nsk.share.gc.ClassChain40* @run main/othervm41* -XX:MaxMetaspaceSize=64M42* -XX:MetaspaceSize=32M43* -XX:CompressedClassSpaceSize=32M44* gc.gctests.LoadUnloadGC.LoadUnloadGC45*/4647package gc.gctests.LoadUnloadGC;4849import nsk.share.test.*;50import nsk.share.gc.*;51import nsk.share.classload.ClassPathNonDelegatingClassLoader;52import vm.share.monitoring.MemoryPoolFinder;5354import java.io.*;55import java.util.*;56import java.lang.management.MemoryPoolMXBean;5758/**59* This test checks that classes are unloaded when loaded multiple times60* with custom class loader.61*/62public class LoadUnloadGC extends ThreadedGCTest {63private final String className = "nsk.share.gc.ClassChain";64private int [] memory_reserve = new int[10000];6566private class Loader implements Runnable {67private Class class_zero_class;68private Object class_zero_object;6970public void run() {71try {72// load Class0 and instantiate it73// This will cause all thousand classes to get loaded74ClassPathNonDelegatingClassLoader loader = new ClassPathNonDelegatingClassLoader();75class_zero_class = loader.loadClass(className, false);76class_zero_object = class_zero_class.newInstance();77// Set all references to null . This should cause a GC78// which should forces an unloading of all these79// unreferenced classes.80class_zero_class = null;81class_zero_object = null;82loader = null;83} catch (ClassNotFoundException e) {84throw new RuntimeException(e);85} catch (InstantiationException e) {86throw new RuntimeException(e);87} catch (IllegalAccessException e) {88throw new RuntimeException(e);89}90}91}9293protected Runnable createRunnable(int i) {94return new Loader();95}9697protected static int getThreadCount() {98MemoryPoolMXBean bean = MemoryPoolFinder.findPool(MemoryPoolFinder.METASPACE);99ClassPathNonDelegatingClassLoader loader = new ClassPathNonDelegatingClassLoader();100long used = bean.getUsage().getUsed();101long free = 0;102int classesCount = 1000;103int classesToLoad = 10;104if(bean.getUsage().getMax() == -1) {105throw new RuntimeException("Metaspace size should be limited for this test.");106}107try {108for(int i = 1; i <= classesToLoad; i++) {109loader.loadClass("nsk.share.gc.Class"+i);110}111} catch (Exception e) {112throw new RuntimeException(e);113}114used = bean.getUsage().getUsed() - used;115free = (bean.getUsage().getMax() - bean.getUsage().getUsed())/2;116return Math.min((int)(0.95*free/(classesCount/classesToLoad*used)),117Runtime.getRuntime().availableProcessors());118}119120public static void main(String args[]) {121int threadCount = getThreadCount();122if (Arrays.binarySearch(args,"-t") < 0) {123args = Arrays.copyOf(args,args.length+2);124args[args.length-2] = "-t";125args[args.length-1] = Integer.toString(threadCount);126}127GC.runTest(new LoadUnloadGC(), args);128}129}130131132