1 /*
   2  * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 5040740
  27  * @summary annotations cause memory leak
  28  * @author gafter
  29  *
  30  * @run shell LoaderLeak.sh
  31  */
  32 
  33 import java.net.*;
  34 import java.lang.ref.*;
  35 import java.util.*;
  36 import java.io.*;
  37 
  38 public class Main {
  39     public static void main(String[] args) throws Exception {
  40         for (int i=0; i<100; i++)
  41             doTest(args.length != 0);
  42     }
  43 
  44     static void doTest(boolean readAnn) throws Exception {
  45         // URL classes = new URL("file://" + System.getProperty("user.dir") + "/classes");
  46         // URL[] path = { classes };
  47         // URLClassLoader loader = new URLClassLoader(path);
  48         ClassLoader loader = new SimpleClassLoader();
  49         WeakReference<Class<?>> c = new WeakReference(loader.loadClass("C"));
  50         if (c.get() == null) throw new AssertionError();
  51         if (c.get().getClassLoader() != loader) throw new AssertionError();
  52         if (readAnn) System.out.println(c.get().getAnnotations()[0]);
  53         if (c.get() == null) throw new AssertionError();
  54         System.gc();
  55         System.gc();
  56         if (c.get() == null) throw new AssertionError();
  57         System.gc();
  58         System.gc();
  59         loader = null;
  60 
  61         // Might require multiple calls to System.gc() for weak-references
  62         // processing to be complete. If the weak-reference is not cleared as
  63         // expected we will hang here until timed out by the test harness.
  64         while (true) {
  65             System.gc();
  66             Thread.sleep(20);
  67             if (c.get() == null) {
  68                 break;
  69             }
  70         }
  71     }
  72 }
  73 
  74 class SimpleClassLoader extends ClassLoader {
  75     private Hashtable classes = new Hashtable();
  76 
  77     public SimpleClassLoader() {
  78     }
  79     private byte getClassImplFromDataBase(String className)[] {
  80         byte result[];
  81         try {
  82             FileInputStream fi = new FileInputStream("classes/"+className+".class");
  83             result = new byte[fi.available()];
  84             fi.read(result);
  85             return result;
  86         } catch (Exception e) {
  87 
  88             /*
  89              * If we caught an exception, either the class wasnt found or it
  90              * was unreadable by our process.
  91              */
  92             return null;
  93         }
  94     }
  95     public Class loadClass(String className) throws ClassNotFoundException {
  96         return (loadClass(className, true));
  97     }
  98     public synchronized Class loadClass(String className, boolean resolveIt)
  99         throws ClassNotFoundException {
 100         Class result;
 101         byte  classData[];
 102 
 103         /* Check our local cache of classes */
 104         result = (Class)classes.get(className);
 105         if (result != null) {
 106             return result;
 107         }
 108 
 109         /* Check with the primordial class loader */
 110         try {
 111             result = super.findSystemClass(className);
 112             return result;
 113         } catch (ClassNotFoundException e) {
 114         }
 115 
 116         /* Try to load it from our repository */
 117         classData = getClassImplFromDataBase(className);
 118         if (classData == null) {
 119             throw new ClassNotFoundException();
 120         }
 121 
 122         /* Define it (parse the class file) */
 123         result = defineClass(classData, 0, classData.length);
 124         if (result == null) {
 125             throw new ClassFormatError();
 126         }
 127 
 128         if (resolveIt) {
 129             resolveClass(result);
 130         }
 131 
 132         classes.put(className, result);
 133         return result;
 134     }
 135 }