LeakTest.java revision 2362
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * Copyright (c) 2006, 2008, Oracle and/or its affiliates. All rights reserved.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * This code is free software; you can redistribute it and/or modify it
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * under the terms of the GNU General Public License version 2 only, as
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * published by the Free Software Foundation.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * This code is distributed in the hope that it will be useful, but WITHOUT
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * version 2 for more details (a copy is included in the LICENSE file that
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * accompanied this code).
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * You should have received a copy of the GNU General Public License version
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * 2 along with this work; if not, write to the Free Software Foundation,
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * or visit www.oracle.com if you need additional information or have any
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * questions.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * @bug 6482247
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * @summary Test that creating MXBeans does not introduce memory leaks.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * @author Eamonn McManus
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * @run build LeakTest RandomMXBeanTest
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * @run main LeakTest
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew/* In this test we create a ClassLoader, then use it to load and run another
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * jtreg test. When the other test has completed, we wait for the ClassLoader
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * to be garbage-collected. If it has not been gc'd after a reasonable
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * amount of time, then something is keeping a reference to the ClassLoader,
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * which implies a memory leak.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * This test can be applied to any jtreg test, not just the MXBean tests.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthewpublic class LeakTest {
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew /* Ideally we would include MXBeanTest in the list of tests, since it
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * has fairly complete coverage. However, the ClassLoader fails to be
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * gc'd when we do that, and I am unable to figure out why. Examining
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * a heap dump shows only weak references to the ClassLoader. I suspect
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * something is wrong in the internals of the reflection classes, used
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew * quite heavily by MXBeanTest.
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew// private static Class<?>[] otherTests = {MXBeanTest.class};
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew private static Class<?>[] otherTests = {RandomMXBeanTest.class};
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew // This class just makes it easier for us to spot our loader in heap dumps
fa3f09215a8dda38ec5a9ce38cf36795d8d6f450matthew private static class ShadowClassLoader extends URLClassLoader {