1 /* 2 * Copyright (c) 2006, 2015, 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 /* @test 25 * @bug 6482247 26 * @summary Test that creating MXBeans does not introduce memory leaks. 27 * @author Eamonn McManus 28 * 29 * @run build LeakTest RandomMXBeanTest MerlinMXBean TigerMXBean 30 * @run main LeakTest 31 */ 32 33 /* In this test we create a ClassLoader, then use it to load and run another 34 * jtreg test. When the other test has completed, we wait for the ClassLoader 35 * to be garbage-collected. If it has not been gc'd after a reasonable 36 * amount of time, then something is keeping a reference to the ClassLoader, 37 * which implies a memory leak. 38 * 39 * This test can be applied to any jtreg test, not just the MXBean tests. 40 */ 41 42 import java.io.File; 43 import java.lang.ref.Reference; 44 import java.lang.ref.ReferenceQueue; 45 import java.lang.ref.WeakReference; 46 import java.lang.reflect.Method; 47 import java.net.URL; 48 import java.net.URLClassLoader; 49 import java.nio.file.Paths; 50 51 public class LeakTest { 52 /* Ideally we would include MXBeanTest in the list of tests, since it 53 * has fairly complete coverage. However, the ClassLoader fails to be 54 * gc'd when we do that, and I am unable to figure out why. Examining 55 * a heap dump shows only weak references to the ClassLoader. I suspect 56 * something is wrong in the internals of the reflection classes, used 57 * quite heavily by MXBeanTest. 58 */ 59 // private static Class<?>[] otherTests = {MXBeanTest.class}; 60 61 private static Class<?>[] otherTests = {RandomMXBeanTest.class}; 62 63 // This class just makes it easier for us to spot our loader in heap dumps 64 private static class ShadowClassLoader extends URLClassLoader { 65 ShadowClassLoader(URL[] urls, ClassLoader parent) { 66 super(urls, parent); 67 } 68 } 69 70 public static void main(String[] args) throws Exception { 71 System.out.println("Testing that no references are held to ClassLoaders " + 72 "by caches in the MXBean infrastructure"); 73 for (Class<?> testClass : otherTests) 74 test(testClass); 75 if (failure != null) 76 throw new Exception("CLASSLOADER LEAK TEST FAILED: " + failure); 77 System.out.println("CLASSLOADER LEAK TEST PASSED"); 78 if (args.length > 0) { 79 System.out.println("Waiting for input"); 80 System.in.read(); 81 } 82 } 83 84 private static void test(Class<?> originalTestClass) throws Exception { 85 System.out.println(); 86 System.out.println("TESTING " + originalTestClass.getName()); 87 WeakReference<ClassLoader> wr = testShadow(originalTestClass); 88 System.out.println("Test passed, waiting for ClassLoader to disappear"); 89 long deadline = System.currentTimeMillis() + 20*1000; 90 Reference<? extends ClassLoader> ref; 91 while (wr.get() != null && System.currentTimeMillis() < deadline) { 92 System.gc(); 93 Thread.sleep(100); 94 } 95 if (wr.get() != null) 96 fail(originalTestClass.getName() + " kept ClassLoader reference"); 97 } 98 99 private static WeakReference<ClassLoader> 100 testShadow(Class<?> originalTestClass) throws Exception { 101 String[] cpaths = System.getProperty("test.classes", ".") 102 .split(File.pathSeparator); 103 URL[] urls = new URL[cpaths.length]; 104 for (int i=0; i < cpaths.length; i++) { 105 urls[i] = Paths.get(cpaths[i]).toUri().toURL(); 106 } 107 108 URLClassLoader shadowLoader = 109 new ShadowClassLoader(urls, originalTestClass.getClassLoader().getParent()); 110 System.out.println("Shadow loader is " + shadowLoader); 111 String className = originalTestClass.getName(); 112 Class<?> testClass = Class.forName(className, false, shadowLoader); 113 if (testClass.getClassLoader() != shadowLoader) { 114 throw new IllegalArgumentException("Loader didn't work: " + 115 testClass.getClassLoader() + " != " + shadowLoader); 116 } 117 Method main = testClass.getMethod("main", String[].class); 118 main.invoke(null, (Object) new String[0]); 119 return new WeakReference<ClassLoader>(shadowLoader); 120 } 121 122 private static void fail(String why) { 123 System.out.println("FAILED: " + why); 124 failure = why; 125 } 126 127 private static String failure; 128 }