jdk/test/javax/management/mxbean/LeakTest.java
author ykantser
Mon, 20 Jul 2015 13:12:00 +0200
changeset 31906 398dc2ff188e
parent 30376 2ccf2cf7ea48
child 33315 d2a313368ccb
permissions -rw-r--r--
8037957: [TEST_BUG] javax/management/mxbean/LeakTest.java misses MerlinMXBean & TigerMXBean in @run build tag Reviewed-by: jbachorik
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
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    28
 * @modules java.desktop
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    29
 *          java.management
31906
398dc2ff188e 8037957: [TEST_BUG] javax/management/mxbean/LeakTest.java misses MerlinMXBean & TigerMXBean in @run build tag
ykantser
parents: 30376
diff changeset
    30
 * @run build LeakTest RandomMXBeanTest MerlinMXBean TigerMXBean
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main LeakTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/* In this test we create a ClassLoader, then use it to load and run another
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * jtreg test.  When the other test has completed, we wait for the ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * to be garbage-collected.  If it has not been gc'd after a reasonable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * amount of time, then something is keeping a reference to the ClassLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * which implies a memory leak.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This test can be applied to any jtreg test, not just the MXBean tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    43
import java.io.File;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.lang.ref.Reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.lang.ref.ReferenceQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.net.URLClassLoader;
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    50
import java.nio.file.Paths;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class LeakTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /* Ideally we would include MXBeanTest in the list of tests, since it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * has fairly complete coverage.  However, the ClassLoader fails to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * gc'd when we do that, and I am unable to figure out why.  Examining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * a heap dump shows only weak references to the ClassLoader.  I suspect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * something is wrong in the internals of the reflection classes, used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * quite heavily by MXBeanTest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
//    private static Class<?>[] otherTests = {MXBeanTest.class};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static Class<?>[] otherTests = {RandomMXBeanTest.class};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // This class just makes it easier for us to spot our loader in heap dumps
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static class ShadowClassLoader extends URLClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        ShadowClassLoader(URL[] urls, ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            super(urls, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        System.out.println("Testing that no references are held to ClassLoaders " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                "by caches in the MXBean infrastructure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        for (Class<?> testClass : otherTests)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            test(testClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (failure != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new Exception("CLASSLOADER LEAK TEST FAILED: " + failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        System.out.println("CLASSLOADER LEAK TEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            System.out.println("Waiting for input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            System.in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static void test(Class<?> originalTestClass) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        System.out.println("TESTING " + originalTestClass.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        WeakReference<ClassLoader> wr = testShadow(originalTestClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        System.out.println("Test passed, waiting for ClassLoader to disappear");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        long deadline = System.currentTimeMillis() + 20*1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        Reference<? extends ClassLoader> ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        while (wr.get() != null && System.currentTimeMillis() < deadline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (wr.get() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            fail(originalTestClass.getName() + " kept ClassLoader reference");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static WeakReference<ClassLoader>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            testShadow(Class<?> originalTestClass) throws Exception {
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
   102
        String[] cpaths = System.getProperty("test.classes", ".")
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
   103
                                .split(File.pathSeparator);
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
   104
        URL[] urls = new URL[cpaths.length];
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
   105
        for (int i=0; i < cpaths.length; i++) {
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
   106
            urls[i] = Paths.get(cpaths[i]).toUri().toURL();
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
   107
        }
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
   108
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        URLClassLoader shadowLoader =
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
   110
                new ShadowClassLoader(urls, originalTestClass.getClassLoader().getParent());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        System.out.println("Shadow loader is " + shadowLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        String className = originalTestClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        Class<?> testClass = Class.forName(className, false, shadowLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (testClass.getClassLoader() != shadowLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new IllegalArgumentException("Loader didn't work: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    testClass.getClassLoader() + " != " + shadowLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        Method main = testClass.getMethod("main", String[].class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        main.invoke(null, (Object) new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return new WeakReference<ClassLoader>(shadowLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private static void fail(String why) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        System.out.println("FAILED: " + why);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        failure = why;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private static String failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
}