Path: blob/master/test/jdk/java/lang/annotation/LoaderLeakTest.java
41149 views
/*1* Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 504074026* @summary annotations cause memory leak27* @library /test/lib28* @build jdk.test.lib.process.*29* @run testng LoaderLeakTest30*/3132import jdk.test.lib.Utils;33import jdk.test.lib.process.ProcessTools;34import org.testng.annotations.BeforeClass;35import org.testng.annotations.Test;36import java.io.FileInputStream;37import java.lang.annotation.Retention;38import java.lang.ref.Reference;39import java.lang.ref.WeakReference;40import java.nio.file.*;41import java.util.*;42import static java.lang.annotation.RetentionPolicy.RUNTIME;4344public class LoaderLeakTest {4546@Test47public void testWithoutReadingAnnotations() throws Throwable {48runJavaProcessExpectSuccessExitCode("Main");49}5051@Test52public void testWithReadingAnnotations() throws Throwable {53runJavaProcessExpectSuccessExitCode("Main", "foo");54}5556private void runJavaProcessExpectSuccessExitCode(String ... command) throws Throwable {57var processBuilder = ProcessTools.createJavaProcessBuilder(command)58.directory(Paths.get(Utils.TEST_CLASSES).toFile());59ProcessTools.executeCommand(processBuilder).shouldHaveExitValue(0);60}6162}6364class Main {6566public static void main(String[] args) throws Exception {67for (int i = 0; i < 100; i++) {68doTest(args.length != 0);69}70}7172static void doTest(boolean readAnn) throws Exception {73ClassLoader loader = new SimpleClassLoader();74var c = new WeakReference<Class<?>>(loader.loadClass("C"));75if (c.refersTo(null)) throw new AssertionError("class missing after loadClass");76// c.get() should never return null here since we hold a strong77// reference to the class loader that loaded the class referred by c.78if (c.get().getClassLoader() != loader) throw new AssertionError("wrong classloader");79if (readAnn) System.out.println(c.get().getAnnotations()[0]);80if (c.refersTo(null)) throw new AssertionError("class missing before GC");81System.gc();82System.gc();83if (c.refersTo(null)) throw new AssertionError("class missing after GC but before loader is unreachable");84System.gc();85System.gc();86Reference.reachabilityFence(loader);87loader = null;8889// Might require multiple calls to System.gc() for weak-references90// processing to be complete. If the weak-reference is not cleared as91// expected we will hang here until timed out by the test harness.92while (true) {93System.gc();94Thread.sleep(20);95if (c.refersTo(null)) {96break;97}98}99}100}101102@Retention(RUNTIME)103@interface A {104B b();105}106107@interface B { }108109@A(b=@B()) class C { }110111class SimpleClassLoader extends ClassLoader {112public SimpleClassLoader() { }113114private byte[] getClassImplFromDataBase(String className) {115try {116return Files.readAllBytes(Paths.get(className + ".class"));117} catch (Exception e) {118throw new Error("could not load class " + className, e);119}120}121122@Override123public Class<?> loadClass(String className, boolean resolveIt)124throws ClassNotFoundException {125switch (className) {126case "A", "B", "C" -> {127var classData = getClassImplFromDataBase(className);128return defineClass(className, classData, 0, classData.length);129}130}131return super.loadClass(className, resolveIt);132}133134}135136137