1 /* |
1 /* |
2 * Copyright (c) 2006, 2008, Oracle and/or its affiliates. All rights reserved. |
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. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
23 |
23 |
24 /* @test |
24 /* @test |
25 * @bug 6482247 |
25 * @bug 6482247 |
26 * @summary Test that creating MXBeans does not introduce memory leaks. |
26 * @summary Test that creating MXBeans does not introduce memory leaks. |
27 * @author Eamonn McManus |
27 * @author Eamonn McManus |
|
28 * @modules java.desktop |
|
29 * java.management |
28 * @run build LeakTest RandomMXBeanTest |
30 * @run build LeakTest RandomMXBeanTest |
29 * @run main LeakTest |
31 * @run main LeakTest |
30 */ |
32 */ |
31 |
33 |
32 /* In this test we create a ClassLoader, then use it to load and run another |
34 /* In this test we create a ClassLoader, then use it to load and run another |
36 * which implies a memory leak. |
38 * which implies a memory leak. |
37 * |
39 * |
38 * This test can be applied to any jtreg test, not just the MXBean tests. |
40 * This test can be applied to any jtreg test, not just the MXBean tests. |
39 */ |
41 */ |
40 |
42 |
|
43 import java.io.File; |
41 import java.lang.ref.Reference; |
44 import java.lang.ref.Reference; |
42 import java.lang.ref.ReferenceQueue; |
45 import java.lang.ref.ReferenceQueue; |
43 import java.lang.ref.WeakReference; |
46 import java.lang.ref.WeakReference; |
44 import java.lang.reflect.Method; |
47 import java.lang.reflect.Method; |
45 import java.net.URL; |
48 import java.net.URL; |
46 import java.net.URLClassLoader; |
49 import java.net.URLClassLoader; |
|
50 import java.nio.file.Paths; |
47 |
51 |
48 public class LeakTest { |
52 public class LeakTest { |
49 /* Ideally we would include MXBeanTest in the list of tests, since it |
53 /* Ideally we would include MXBeanTest in the list of tests, since it |
50 * has fairly complete coverage. However, the ClassLoader fails to be |
54 * 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 |
55 * gc'd when we do that, and I am unable to figure out why. Examining |
93 fail(originalTestClass.getName() + " kept ClassLoader reference"); |
97 fail(originalTestClass.getName() + " kept ClassLoader reference"); |
94 } |
98 } |
95 |
99 |
96 private static WeakReference<ClassLoader> |
100 private static WeakReference<ClassLoader> |
97 testShadow(Class<?> originalTestClass) throws Exception { |
101 testShadow(Class<?> originalTestClass) throws Exception { |
98 URLClassLoader originalLoader = |
102 String[] cpaths = System.getProperty("test.classes", ".") |
99 (URLClassLoader) originalTestClass.getClassLoader(); |
103 .split(File.pathSeparator); |
100 URL[] urls = originalLoader.getURLs(); |
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 |
101 URLClassLoader shadowLoader = |
109 URLClassLoader shadowLoader = |
102 new ShadowClassLoader(urls, originalLoader.getParent()); |
110 new ShadowClassLoader(urls, originalTestClass.getClassLoader().getParent()); |
103 System.out.println("Shadow loader is " + shadowLoader); |
111 System.out.println("Shadow loader is " + shadowLoader); |
104 String className = originalTestClass.getName(); |
112 String className = originalTestClass.getName(); |
105 Class<?> testClass = Class.forName(className, false, shadowLoader); |
113 Class<?> testClass = Class.forName(className, false, shadowLoader); |
106 if (testClass.getClassLoader() != shadowLoader) { |
114 if (testClass.getClassLoader() != shadowLoader) { |
107 throw new IllegalArgumentException("Loader didn't work: " + |
115 throw new IllegalArgumentException("Loader didn't work: " + |