1 /* 2 * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @key stress gc 27 * 28 * @summary converted from VM Testbase gc/gctests/LoadUnloadGC. 29 * VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, monitoring] 30 * VM Testbase readme: 31 * In this test a 1000 classes are loaded and unloaded in a loop. 32 * Class0 gets loaded which results in Class1 getting loaded and so on all 33 * the way uptill class1000. The classes should be unloaded whenever a 34 * garbage collection takes place because their classloader is made unreachable 35 * at the end of the each loop iteration. The loop is repeated 1000 times. 36 * 37 * @library /vmTestbase 38 * /test/lib 39 * @run driver jdk.test.lib.FileInstaller . . 40 * @build nsk.share.gc.ClassChain 41 * @run main/othervm 42 * -XX:MaxMetaspaceSize=64M 43 * -XX:MetaspaceSize=32M 44 * -XX:CompressedClassSpaceSize=32M 45 * gc.gctests.LoadUnloadGC.LoadUnloadGC 46 */ 47 48 package gc.gctests.LoadUnloadGC; 49 50 import nsk.share.test.*; 51 import nsk.share.gc.*; 52 import nsk.share.classload.ClassPathNonDelegatingClassLoader; 53 import vm.share.monitoring.MemoryPoolFinder; 54 55 import java.io.*; 56 import java.util.*; 57 import java.lang.management.MemoryPoolMXBean; 58 59 /** 60 * This test checks that classes are unloaded when loaded multiple times 61 * with custom class loader. 62 */ 63 public class LoadUnloadGC extends ThreadedGCTest { 64 private final String className = "nsk.share.gc.ClassChain"; 65 private int [] memory_reserve = new int[10000]; 66 67 private class Loader implements Runnable { 68 private Class class_zero_class; 69 private Object class_zero_object; 70 71 public void run() { 72 try { 73 // load Class0 and instantiate it 74 // This will cause all thousand classes to get loaded 75 ClassPathNonDelegatingClassLoader loader = new ClassPathNonDelegatingClassLoader(); 76 class_zero_class = loader.loadClass(className, false); 77 class_zero_object = class_zero_class.newInstance(); 78 // Set all references to null . This should cause a GC 79 // which should forces an unloading of all these 80 // unreferenced classes. 81 class_zero_class = null; 82 class_zero_object = null; 83 loader = null; 84 } catch (ClassNotFoundException e) { 85 throw new RuntimeException(e); 86 } catch (InstantiationException e) { 87 throw new RuntimeException(e); 88 } catch (IllegalAccessException e) { 89 throw new RuntimeException(e); 90 } 91 } 92 } 93 94 protected Runnable createRunnable(int i) { 95 return new Loader(); 96 } 97 98 protected static int getThreadCount() { 99 MemoryPoolMXBean bean = MemoryPoolFinder.findPool(MemoryPoolFinder.METASPACE); 100 ClassPathNonDelegatingClassLoader loader = new ClassPathNonDelegatingClassLoader(); 101 long used = bean.getUsage().getUsed(); 102 long free = 0; 103 int classesCount = 1000; 104 int classesToLoad = 10; 105 if(bean.getUsage().getMax() == -1) { 106 throw new RuntimeException("Metaspace size should be limited for this test."); 107 } 108 try { 109 for(int i = 1; i <= classesToLoad; i++) { 110 loader.loadClass("nsk.share.gc.Class"+i); 111 } 112 } catch (Exception e) { 113 throw new RuntimeException(e); 114 } 115 used = bean.getUsage().getUsed() - used; 116 free = (bean.getUsage().getMax() - bean.getUsage().getUsed())/2; 117 return Math.min((int)(0.95*free/(classesCount/classesToLoad*used)), 118 Runtime.getRuntime().availableProcessors()); 119 } 120 121 public static void main(String args[]) { 122 int threadCount = getThreadCount(); 123 if (Arrays.binarySearch(args,"-t") < 0) { 124 args = Arrays.copyOf(args,args.length+2); 125 args[args.length-2] = "-t"; 126 args[args.length-1] = Integer.toString(threadCount); 127 } 128 GC.runTest(new LoadUnloadGC(), args); 129 } 130 }