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