|
1 /* |
|
2 * Copyright (c) 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 package jdk.jfr.jmx; |
|
26 |
|
27 import java.lang.management.ManagementFactory; |
|
28 import java.net.URL; |
|
29 import java.net.URLClassLoader; |
|
30 |
|
31 import javax.management.MBeanAttributeInfo; |
|
32 import javax.management.MBeanInfo; |
|
33 import javax.management.MBeanServer; |
|
34 import javax.management.ObjectName; |
|
35 |
|
36 import jdk.jfr.Recording; |
|
37 import jdk.jfr.consumer.RecordedClassLoader; |
|
38 import jdk.jfr.consumer.RecordedEvent; |
|
39 import jdk.management.jfr.FlightRecorderMXBean; |
|
40 import jdk.test.lib.jfr.Events; |
|
41 |
|
42 /** |
|
43 * @test |
|
44 * @key jfr |
|
45 * @summary Verifies that attributes in FlightRecorderMXBean can be inspected |
|
46 * without causing a memory leak. |
|
47 * @requires vm.hasJFR |
|
48 * @library /test/lib /test/jdk |
|
49 * @run main/othervm jdk.jfr.jmx.TestFlightRecorderMXBeanLeak |
|
50 */ |
|
51 public class TestFlightRecorderMXBeanLeak { |
|
52 |
|
53 private static final String CLASS_LOADER_NAME = "Test Leak"; |
|
54 private static final String TEST_CLASS = "jdk.jfr.jmx.TestFlightRecorderMXBeanLeak$FlightRecorderMXBeanLoader"; |
|
55 |
|
56 public static void main(String[] args) throws Exception { |
|
57 URL url = FlightRecorderMXBeanLoader.class.getProtectionDomain().getCodeSource().getLocation(); |
|
58 URLClassLoader loader = new URLClassLoader(CLASS_LOADER_NAME, new URL[] { url }, null); |
|
59 Class<?> clazz = Class.forName(TEST_CLASS, true, loader); |
|
60 clazz.newInstance(); |
|
61 loader.close(); |
|
62 clazz = null; |
|
63 loader = null; |
|
64 System.gc(); |
|
65 System.gc(); |
|
66 |
|
67 try (Recording r = new Recording()) { |
|
68 r.enable("jdk.ClassLoaderStatistics").with("period", "endChunk"); |
|
69 r.start(); |
|
70 r.stop(); |
|
71 for (RecordedEvent e : Events.fromRecording(r)) { |
|
72 RecordedClassLoader o = e.getValue("classLoader"); |
|
73 if (o != null) { |
|
74 System.out.println("Class loader: type=" + o.getType().getName() + " name=" + o.getName()); |
|
75 if (CLASS_LOADER_NAME.equals(o.getName())) { |
|
76 throw new Exception("Memory Leak. Class loader '" + CLASS_LOADER_NAME + "' should not be on the heap!"); |
|
77 } |
|
78 } |
|
79 } |
|
80 } |
|
81 } |
|
82 |
|
83 public static class FlightRecorderMXBeanLoader { |
|
84 public FlightRecorderMXBeanLoader() throws Exception { |
|
85 ObjectName objectName = new ObjectName(FlightRecorderMXBean.MXBEAN_NAME); |
|
86 MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer(); |
|
87 MBeanInfo mBeanInfo = platformMBeanServer.getMBeanInfo(objectName); |
|
88 System.out.println("Inspecting FlightRecorderMXBeann:"); |
|
89 for (MBeanAttributeInfo attributeInfo : mBeanInfo.getAttributes()) { |
|
90 Object value = platformMBeanServer.getAttribute(objectName, attributeInfo.getName()); |
|
91 System.out.println(attributeInfo.getName() + " = " + value); |
|
92 } |
|
93 } |
|
94 } |
|
95 } |