Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/management/mxbean/LeakTest.java
41149 views
1
/*
2
* Copyright (c) 2006, 2018, 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
* @requires vm.opt.final.ClassUnloading
28
* @author Eamonn McManus
29
*
30
* @run build LeakTest RandomMXBeanTest MerlinMXBean TigerMXBean
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
71
public static void main(String[] args) throws Exception {
72
System.out.println("Testing that no references are held to ClassLoaders " +
73
"by caches in the MXBean infrastructure");
74
for (Class<?> testClass : otherTests)
75
test(testClass);
76
if (failure != null)
77
throw new Exception("CLASSLOADER LEAK TEST FAILED: " + failure);
78
System.out.println("CLASSLOADER LEAK TEST PASSED");
79
if (args.length > 0) {
80
System.out.println("Waiting for input");
81
System.in.read();
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
}
130
131