Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/annotation/loaderLeak/Main.java
38828 views
/*1* Copyright (c) 2004, 2012, 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* @author gafter28*29* @run shell LoaderLeak.sh30*/3132import java.net.*;33import java.lang.ref.*;34import java.util.*;35import java.io.*;3637public class Main {38public static void main(String[] args) throws Exception {39for (int i=0; i<100; i++)40doTest(args.length != 0);41}4243static void doTest(boolean readAnn) throws Exception {44// URL classes = new URL("file://" + System.getProperty("user.dir") + "/classes");45// URL[] path = { classes };46// URLClassLoader loader = new URLClassLoader(path);47ClassLoader loader = new SimpleClassLoader();48WeakReference<Class<?>> c = new WeakReference(loader.loadClass("C"));49if (c.get() == null) throw new AssertionError();50if (c.get().getClassLoader() != loader) throw new AssertionError();51if (readAnn) System.out.println(c.get().getAnnotations()[0]);52if (c.get() == null) throw new AssertionError();53System.gc();54System.gc();55if (c.get() == null) throw new AssertionError();56System.gc();57System.gc();58loader = null;5960// Might require multiple calls to System.gc() for weak-references61// processing to be complete. If the weak-reference is not cleared as62// expected we will hang here until timed out by the test harness.63while (true) {64System.gc();65Thread.sleep(20);66if (c.get() == null) {67break;68}69}70}71}7273class SimpleClassLoader extends ClassLoader {74private Hashtable classes = new Hashtable();7576public SimpleClassLoader() {77}78private byte getClassImplFromDataBase(String className)[] {79byte result[];80try {81FileInputStream fi = new FileInputStream("classes/"+className+".class");82result = new byte[fi.available()];83fi.read(result);84return result;85} catch (Exception e) {8687/*88* If we caught an exception, either the class wasnt found or it89* was unreadable by our process.90*/91return null;92}93}94public Class loadClass(String className) throws ClassNotFoundException {95return (loadClass(className, true));96}97public synchronized Class loadClass(String className, boolean resolveIt)98throws ClassNotFoundException {99Class result;100byte classData[];101102/* Check our local cache of classes */103result = (Class)classes.get(className);104if (result != null) {105return result;106}107108/* Check with the primordial class loader */109try {110result = super.findSystemClass(className);111return result;112} catch (ClassNotFoundException e) {113}114115/* Try to load it from our repository */116classData = getClassImplFromDataBase(className);117if (classData == null) {118throw new ClassNotFoundException();119}120121/* Define it (parse the class file) */122result = defineClass(classData, 0, classData.length);123if (result == null) {124throw new ClassFormatError();125}126127if (resolveIt) {128resolveClass(result);129}130131classes.put(className, result);132return result;133}134}135136137