1 /* 2 * Copyright (c) 2016, 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 /* 26 * @test ClassUnloadTest 27 * @bug 8142506 28 * @library /testlibrary 29 * @run driver ClassUnloadTest 30 */ 31 32 import jdk.test.lib.*; 33 import java.lang.ref.WeakReference; 34 import java.lang.reflect.Method; 35 36 public class ClassUnloadTest { 37 private static OutputAnalyzer out; 38 private static ProcessBuilder pb; 39 private static class ClassUnloadTestMain { 40 41 static String klass = "public class Empty {" 42 + " public void bigLoop(){}" 43 + " public String toString() { return \"nothing\"; } }"; 44 45 public static volatile WeakReference<ByteCodeLoader> clweak; 46 public static byte[] garbage; 47 48 public static void main(String... args) throws Exception { 49 ByteCodeLoader cl = new ByteCodeLoader("Empty", InMemoryJavaCompiler.compile("Empty", klass)); 50 Class<?> c = cl.loadClass("Empty"); 51 Object o = c.newInstance(); 52 Method m = c.getMethod("bigLoop"); 53 54 for ( int i=0; i<10001; i++){ 55 m.invoke(o); 56 } 57 58 clweak = new WeakReference<>(cl); 59 cl = null; c = null; o = null; m = null; 60 while(clweak.get() != null) { 61 garbage = new byte[8192]; 62 System.gc(); 63 } 64 } 65 } 66 67 static void checkFor(String... outputStrings) throws Exception { 68 out = new OutputAnalyzer(pb.start()); 69 for (String s: outputStrings) { 70 out.shouldContain(s); 71 } 72 out.shouldHaveExitValue(0); 73 out.getOutput(); 74 } 75 76 static void checkAbsent(String... outputStrings) throws Exception { 77 out = new OutputAnalyzer(pb.start()); 78 for (String s: outputStrings) { 79 out.shouldNotContain(s); 80 } 81 out.shouldHaveExitValue(0); 82 } 83 84 public static void main(String... args) throws Exception { 85 86 // (1) -Xlog:classunload=info 87 pb = ProcessTools.createJavaProcessBuilder( 88 "-Xlog:classunload=info", "-Xmx64m", ClassUnloadTestMain.class.getName() 89 ); 90 checkFor("[classunload]", "[Unloading class"); 91 92 // (2) -Xlog:classunload=trace 93 pb = ProcessTools.createJavaProcessBuilder( 94 "-Xlog:classunload=trace", "-Xmx64m", ClassUnloadTestMain.class.getName() 95 ); 96 checkFor("[classunload]", "Class unloading: Making nmethod"); 97 98 // (3) -Xlog:classunload=off 99 pb = ProcessTools.createJavaProcessBuilder( 100 "-Xlog:classunload=off", "-Xmx64m", ClassUnloadTestMain.class.getName() 101 ); 102 checkAbsent("[classunload]"); 103 104 // (4) -XX:+TraceClassUnloading 105 pb = ProcessTools.createJavaProcessBuilder( 106 "-XX:+TraceClassUnloading", "-Xmx64m", ClassUnloadTestMain.class.getName() 107 ); 108 checkFor("[classunload]", "[Unloading class"); 109 110 // (5) -XX:-TraceClassUnloading 111 pb = ProcessTools.createJavaProcessBuilder( 112 "-XX:-TraceClassUnloading", "-Xmx64m", ClassUnloadTestMain.class.getName() 113 ); 114 checkAbsent("[classunload]"); 115 116 // (6) -Xlog:classload=info 117 pb = ProcessTools.createJavaProcessBuilder( 118 "-Xlog:classload=info", "-Xmx64m", ClassUnloadTestMain.class.getName() 119 ); 120 checkFor("[classload]", "java.lang.Object", "source:"); 121 122 // (7) -Xlog:classload=debug 123 pb = ProcessTools.createJavaProcessBuilder( 124 "-Xlog:classload=debug", "-Xmx64m", ClassUnloadTestMain.class.getName() 125 ); 126 checkFor("[classload]", "java.lang.Object", "source:", "klass:", "super:", "loader:", "bytes:"); 127 128 // (8) -Xlog:classload=off 129 pb = ProcessTools.createJavaProcessBuilder( 130 "-Xlog:classload=off", "-Xmx64m", ClassUnloadTestMain.class.getName() 131 ); 132 checkAbsent("[classload]"); 133 134 // (9) -XX:+TraceClassLoading 135 pb = ProcessTools.createJavaProcessBuilder( 136 "-XX:+TraceClassLoading", "-Xmx64m", ClassUnloadTestMain.class.getName() 137 ); 138 checkFor("[classload]", "java.lang.Object", "source:"); 139 140 // (10) -XX:-TraceClassLoading 141 pb = ProcessTools.createJavaProcessBuilder( 142 "-XX:-TraceClassLoading", "-Xmx64m", ClassUnloadTestMain.class.getName() 143 ); 144 checkAbsent("[classload]"); 145 146 // (11) -verbose:class 147 pb = ProcessTools.createJavaProcessBuilder( 148 "-verbose:class", "-Xmx64m", ClassUnloadTestMain.class.getName() 149 ); 150 checkFor("[classload]", "java.lang.Object", "source:"); 151 checkFor("[classunload]", "[Unloading class"); 152 153 // (12) -Xlog:classloaderdata=trace 154 pb = ProcessTools.createJavaProcessBuilder( 155 "-Xlog:classloaderdata=trace", "-Xmx64m", ClassUnloadTestMain.class.getName() 156 ); 157 checkFor("[classloaderdata]", "create class loader data"); 158 159 } 160 }