jdk/test/javax/management/mxbean/LeakTest.java
author tschatzl
Wed, 16 Apr 2014 10:55:26 +0200
changeset 24100 7e71ac14ec06
parent 5506 202f599c92aa
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
8027553: Change the in_cset_fast_test functionality to use the G1BiasedArray abstraction Summary: Instead of using a manually managed array for the in_cset_fast_test array, use a G1BiasedArray instance. Reviewed-by: brutisso, mgerdin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     2
 * Copyright (c) 2006, 2008, 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
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    28
 * @run build LeakTest RandomMXBeanTest
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run main LeakTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/* In this test we create a ClassLoader, then use it to load and run another
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * jtreg test.  When the other test has completed, we wait for the ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * to be garbage-collected.  If it has not been gc'd after a reasonable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * amount of time, then something is keeping a reference to the ClassLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * which implies a memory leak.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This test can be applied to any jtreg test, not just the MXBean tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.lang.ref.Reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.lang.ref.ReferenceQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.net.URLClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class LeakTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /* Ideally we would include MXBeanTest in the list of tests, since it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * has fairly complete coverage.  However, the ClassLoader fails to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * gc'd when we do that, and I am unable to figure out why.  Examining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * a heap dump shows only weak references to the ClassLoader.  I suspect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * something is wrong in the internals of the reflection classes, used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * quite heavily by MXBeanTest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
//    private static Class<?>[] otherTests = {MXBeanTest.class};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static Class<?>[] otherTests = {RandomMXBeanTest.class};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // This class just makes it easier for us to spot our loader in heap dumps
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static class ShadowClassLoader extends URLClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        ShadowClassLoader(URL[] urls, ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            super(urls, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        System.out.println("Testing that no references are held to ClassLoaders " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                "by caches in the MXBean infrastructure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        for (Class<?> testClass : otherTests)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            test(testClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (failure != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new Exception("CLASSLOADER LEAK TEST FAILED: " + failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        System.out.println("CLASSLOADER LEAK TEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            System.out.println("Waiting for input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            System.in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static void test(Class<?> originalTestClass) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        System.out.println("TESTING " + originalTestClass.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        WeakReference<ClassLoader> wr = testShadow(originalTestClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        System.out.println("Test passed, waiting for ClassLoader to disappear");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        long deadline = System.currentTimeMillis() + 20*1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Reference<? extends ClassLoader> ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        while (wr.get() != null && System.currentTimeMillis() < deadline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (wr.get() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            fail(originalTestClass.getName() + " kept ClassLoader reference");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static WeakReference<ClassLoader>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            testShadow(Class<?> originalTestClass) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        URLClassLoader originalLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                (URLClassLoader) originalTestClass.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        URL[] urls = originalLoader.getURLs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        URLClassLoader shadowLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                new ShadowClassLoader(urls, originalLoader.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        System.out.println("Shadow loader is " + shadowLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        String className = originalTestClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        Class<?> testClass = Class.forName(className, false, shadowLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (testClass.getClassLoader() != shadowLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throw new IllegalArgumentException("Loader didn't work: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    testClass.getClassLoader() + " != " + shadowLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Method main = testClass.getMethod("main", String[].class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        main.invoke(null, (Object) new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return new WeakReference<ClassLoader>(shadowLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private static void fail(String why) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        System.out.println("FAILED: " + why);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        failure = why;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static String failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
}