LeakTest.java revision 2362
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew/*
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * Copyright (c) 2006, 2008, Oracle and/or its affiliates. All rights reserved.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew *
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * This code is free software; you can redistribute it and/or modify it
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * under the terms of the GNU General Public License version 2 only, as
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * published by the Free Software Foundation.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew *
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * This code is distributed in the hope that it will be useful, but WITHOUT
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * version 2 for more details (a copy is included in the LICENSE file that
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * accompanied this code).
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew *
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * You should have received a copy of the GNU General Public License version
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * 2 along with this work; if not, write to the Free Software Foundation,
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew *
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * or visit www.oracle.com if you need additional information or have any
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * questions.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew */
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew/* @test
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * @bug 6482247
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * @summary Test that creating MXBeans does not introduce memory leaks.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * @author Eamonn McManus
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * @run build LeakTest RandomMXBeanTest
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * @run main LeakTest
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew */
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew/* In this test we create a ClassLoader, then use it to load and run another
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * jtreg test. When the other test has completed, we wait for the ClassLoader
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * to be garbage-collected. If it has not been gc'd after a reasonable
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * amount of time, then something is keeping a reference to the ClassLoader,
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * which implies a memory leak.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew *
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * This test can be applied to any jtreg test, not just the MXBean tests.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew */
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthewimport java.lang.ref.Reference;
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthewimport java.lang.ref.ReferenceQueue;
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthewimport java.lang.ref.WeakReference;
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthewimport java.lang.reflect.Method;
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthewimport java.net.URL;
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthewimport java.net.URLClassLoader;
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthewpublic class LeakTest {
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew /* Ideally we would include MXBeanTest in the list of tests, since it
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * has fairly complete coverage. However, the ClassLoader fails to be
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * gc'd when we do that, and I am unable to figure out why. Examining
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * a heap dump shows only weak references to the ClassLoader. I suspect
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * something is wrong in the internals of the reflection classes, used
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * quite heavily by MXBeanTest.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew */
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew// private static Class<?>[] otherTests = {MXBeanTest.class};
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew private static Class<?>[] otherTests = {RandomMXBeanTest.class};
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew // This class just makes it easier for us to spot our loader in heap dumps
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew private static class ShadowClassLoader extends URLClassLoader {
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew ShadowClassLoader(URL[] urls, ClassLoader parent) {
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew super(urls, parent);
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew }
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew }
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew public static void main(String[] args) throws Exception {
System.out.println("Testing that no references are held to ClassLoaders " +
"by caches in the MXBean infrastructure");
for (Class<?> testClass : otherTests)
test(testClass);
if (failure != null)
throw new Exception("CLASSLOADER LEAK TEST FAILED: " + failure);
System.out.println("CLASSLOADER LEAK TEST PASSED");
if (args.length > 0) {
System.out.println("Waiting for input");
System.in.read();
}
}
private static void test(Class<?> originalTestClass) throws Exception {
System.out.println();
System.out.println("TESTING " + originalTestClass.getName());
WeakReference<ClassLoader> wr = testShadow(originalTestClass);
System.out.println("Test passed, waiting for ClassLoader to disappear");
long deadline = System.currentTimeMillis() + 20*1000;
Reference<? extends ClassLoader> ref;
while (wr.get() != null && System.currentTimeMillis() < deadline) {
System.gc();
Thread.sleep(100);
}
if (wr.get() != null)
fail(originalTestClass.getName() + " kept ClassLoader reference");
}
private static WeakReference<ClassLoader>
testShadow(Class<?> originalTestClass) throws Exception {
URLClassLoader originalLoader =
(URLClassLoader) originalTestClass.getClassLoader();
URL[] urls = originalLoader.getURLs();
URLClassLoader shadowLoader =
new ShadowClassLoader(urls, originalLoader.getParent());
System.out.println("Shadow loader is " + shadowLoader);
String className = originalTestClass.getName();
Class<?> testClass = Class.forName(className, false, shadowLoader);
if (testClass.getClassLoader() != shadowLoader) {
throw new IllegalArgumentException("Loader didn't work: " +
testClass.getClassLoader() + " != " + shadowLoader);
}
Method main = testClass.getMethod("main", String[].class);
main.invoke(null, (Object) new String[0]);
return new WeakReference<ClassLoader>(shadowLoader);
}
private static void fail(String why) {
System.out.println("FAILED: " + why);
failure = why;
}
private static String failure;
}