author | lfoltan |
Tue, 20 Feb 2018 07:46:40 -0500 | |
changeset 49026 | 844bf1deff1a |
parent 47216 | 71c04702a3d5 |
child 50405 | 449cbde4c40c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
25 |
* @bug 6482247 |
|
26 |
* @summary Test that creating MXBeans does not introduce memory leaks. |
|
27 |
* @author Eamonn McManus |
|
44423 | 28 |
* |
31906
398dc2ff188e
8037957: [TEST_BUG] javax/management/mxbean/LeakTest.java misses MerlinMXBean & TigerMXBean in @run build tag
ykantser
parents:
30376
diff
changeset
|
29 |
* @run build LeakTest RandomMXBeanTest MerlinMXBean TigerMXBean |
2 | 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 |
||
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
5506
diff
changeset
|
42 |
import java.io.File; |
2 | 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; |
|
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
5506
diff
changeset
|
49 |
import java.nio.file.Paths; |
2 | 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 { |
|
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
5506
diff
changeset
|
101 |
String[] cpaths = System.getProperty("test.classes", ".") |
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
5506
diff
changeset
|
102 |
.split(File.pathSeparator); |
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
5506
diff
changeset
|
103 |
URL[] urls = new URL[cpaths.length]; |
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
5506
diff
changeset
|
104 |
for (int i=0; i < cpaths.length; i++) { |
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
5506
diff
changeset
|
105 |
urls[i] = Paths.get(cpaths[i]).toUri().toURL(); |
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
5506
diff
changeset
|
106 |
} |
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
5506
diff
changeset
|
107 |
|
2 | 108 |
URLClassLoader shadowLoader = |
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
5506
diff
changeset
|
109 |
new ShadowClassLoader(urls, originalTestClass.getClassLoader().getParent()); |
2 | 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 |
} |