jdk/test/javax/management/mxbean/LeakTest.java
author jbachorik
Wed, 23 Sep 2015 14:25:02 +0200
changeset 33499 d4f084ec39eb
parent 33316 7a1a203436f7
child 44423 306c020eb154
permissions -rw-r--r--
8139727: Define ConstructorParameters annotation type for MXBeans Reviewed-by: alanb, mchung, dfuchs, abuckley, plevart, mr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 6482247
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test that creating MXBeans does not introduce memory leaks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Eamonn McManus
33499
d4f084ec39eb 8139727: Define ConstructorParameters annotation type for MXBeans
jbachorik
parents: 33316
diff changeset
    28
 * @modules java.management
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run main LeakTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/* In this test we create a ClassLoader, then use it to load and run another
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * jtreg test.  When the other test has completed, we wait for the ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * to be garbage-collected.  If it has not been gc'd after a reasonable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * amount of time, then something is keeping a reference to the ClassLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * which implies a memory leak.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This test can be applied to any jtreg test, not just the MXBean tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    42
import java.io.File;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.lang.ref.Reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.lang.ref.ReferenceQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class LeakTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /* Ideally we would include MXBeanTest in the list of tests, since it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * has fairly complete coverage.  However, the ClassLoader fails to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * gc'd when we do that, and I am unable to figure out why.  Examining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * a heap dump shows only weak references to the ClassLoader.  I suspect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * something is wrong in the internals of the reflection classes, used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * quite heavily by MXBeanTest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
//    private static Class<?>[] otherTests = {MXBeanTest.class};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static Class<?>[] otherTests = {RandomMXBeanTest.class};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // This class just makes it easier for us to spot our loader in heap dumps
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static class ShadowClassLoader extends URLClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        ShadowClassLoader(URL[] urls, ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            super(urls, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        System.out.println("Testing that no references are held to ClassLoaders " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                "by caches in the MXBean infrastructure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        for (Class<?> testClass : otherTests)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            test(testClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (failure != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new Exception("CLASSLOADER LEAK TEST FAILED: " + failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        System.out.println("CLASSLOADER LEAK TEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            System.out.println("Waiting for input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            System.in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static void test(Class<?> originalTestClass) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        System.out.println("TESTING " + originalTestClass.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        WeakReference<ClassLoader> wr = testShadow(originalTestClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        System.out.println("Test passed, waiting for ClassLoader to disappear");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        long deadline = System.currentTimeMillis() + 20*1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        Reference<? extends ClassLoader> ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        while (wr.get() != null && System.currentTimeMillis() < deadline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (wr.get() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            fail(originalTestClass.getName() + " kept ClassLoader reference");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static WeakReference<ClassLoader>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        System.out.println("Shadow loader is " + shadowLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        String className = originalTestClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        Class<?> testClass = Class.forName(className, false, shadowLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (testClass.getClassLoader() != shadowLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw new IllegalArgumentException("Loader didn't work: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    testClass.getClassLoader() + " != " + shadowLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        Method main = testClass.getMethod("main", String[].class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        main.invoke(null, (Object) new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return new WeakReference<ClassLoader>(shadowLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static void fail(String why) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        System.out.println("FAILED: " + why);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        failure = why;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private static String failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
}