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