--- /dev/null 2015-11-21 16:36:38.509275105 -0500 +++ new/test/runtime/logging/ClassLoadUnload.java 2016-01-08 01:43:06.798198474 -0500 @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + + +/* + * @test ClassUnloadTest + * @bug 8142506 + * @library /testlibrary + * @run driver ClassUnloadTest + */ + +import jdk.test.lib.*; +import java.lang.ref.WeakReference; +import java.lang.reflect.Method; + +public class ClassUnloadTest { + private static OutputAnalyzer out; + private static ProcessBuilder pb; + private static class ClassUnloadTestMain { + + static String klass = "public class Empty {" + + " public void bigLoop(){}" + + " public String toString() { return \"nothing\"; } }"; + + public static volatile WeakReference clweak; + public static byte[] garbage; + + public static void main(String... args) throws Exception { + ByteCodeLoader cl = new ByteCodeLoader("Empty", InMemoryJavaCompiler.compile("Empty", klass)); + Class c = cl.loadClass("Empty"); + Object o = c.newInstance(); + Method m = c.getMethod("bigLoop"); + + for ( int i=0; i<10001; i++){ + m.invoke(o); + } + + clweak = new WeakReference<>(cl); + cl = null; c = null; o = null; m = null; + while(clweak.get() != null) { + garbage = new byte[8192]; + System.gc(); + } + } + } + + static void checkFor(String... outputStrings) throws Exception { + out = new OutputAnalyzer(pb.start()); + for (String s: outputStrings) { + out.shouldContain(s); + } + out.shouldHaveExitValue(0); + out.getOutput(); + } + + static void checkAbsent(String... outputStrings) throws Exception { + out = new OutputAnalyzer(pb.start()); + for (String s: outputStrings) { + out.shouldNotContain(s); + } + out.shouldHaveExitValue(0); + } + + public static void main(String... args) throws Exception { + + // (1) -Xlog:classunload=info + pb = ProcessTools.createJavaProcessBuilder( + "-Xlog:classunload=info", "-Xmx64m", ClassUnloadTestMain.class.getName() + ); + checkFor("[classunload]", "[Unloading class"); + + // (2) -Xlog:classunload=trace + pb = ProcessTools.createJavaProcessBuilder( + "-Xlog:classunload=trace", "-Xmx64m", ClassUnloadTestMain.class.getName() + ); + checkFor("[classunload]", "Class unloading: Making nmethod"); + + // (3) -Xlog:classunload=off + pb = ProcessTools.createJavaProcessBuilder( + "-Xlog:classunload=off", "-Xmx64m", ClassUnloadTestMain.class.getName() + ); + checkAbsent("[classunload]"); + + // (4) -XX:+TraceClassUnloading + pb = ProcessTools.createJavaProcessBuilder( + "-XX:+TraceClassUnloading", "-Xmx64m", ClassUnloadTestMain.class.getName() + ); + checkFor("[classunload]", "[Unloading class"); + + // (5) -XX:-TraceClassUnloading + pb = ProcessTools.createJavaProcessBuilder( + "-XX:-TraceClassUnloading", "-Xmx64m", ClassUnloadTestMain.class.getName() + ); + checkAbsent("[classunload]"); + + // (6) -Xlog:classload=info + pb = ProcessTools.createJavaProcessBuilder( + "-Xlog:classload=info", "-Xmx64m", ClassUnloadTestMain.class.getName() + ); + checkFor("[classload]", "java.lang.Object", "source:"); + + // (7) -Xlog:classload=debug + pb = ProcessTools.createJavaProcessBuilder( + "-Xlog:classload=debug", "-Xmx64m", ClassUnloadTestMain.class.getName() + ); + checkFor("[classload]", "java.lang.Object", "source:", "klass:", "super:", "loader:", "bytes:"); + + // (8) -Xlog:classload=off + pb = ProcessTools.createJavaProcessBuilder( + "-Xlog:classload=off", "-Xmx64m", ClassUnloadTestMain.class.getName() + ); + checkAbsent("[classload]"); + + // (9) -XX:+TraceClassLoading + pb = ProcessTools.createJavaProcessBuilder( + "-XX:+TraceClassLoading", "-Xmx64m", ClassUnloadTestMain.class.getName() + ); + checkFor("[classload]", "java.lang.Object", "source:"); + + // (10) -XX:-TraceClassLoading + pb = ProcessTools.createJavaProcessBuilder( + "-XX:-TraceClassLoading", "-Xmx64m", ClassUnloadTestMain.class.getName() + ); + checkAbsent("[classload]"); + + // (11) -verbose:class + pb = ProcessTools.createJavaProcessBuilder( + "-verbose:class", "-Xmx64m", ClassUnloadTestMain.class.getName() + ); + checkFor("[classload]", "java.lang.Object", "source:"); + checkFor("[classunload]", "[Unloading class"); + + // (12) -Xlog:classloaderdata=trace + pb = ProcessTools.createJavaProcessBuilder( + "-Xlog:classloaderdata=trace", "-Xmx64m", ClassUnloadTestMain.class.getName() + ); + checkFor("[classloaderdata]", "create class loader data"); + + } +}