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 */
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 }
|
1 /*
2 * Copyright (c) 2004, 2019, 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 */
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 Reference.reachabilityFence(loader);
60 loader = null;
61
62 // Might require multiple calls to System.gc() for weak-references
63 // processing to be complete. If the weak-reference is not cleared as
64 // expected we will hang here until timed out by the test harness.
65 while (true) {
66 System.gc();
67 Thread.sleep(20);
68 if (c.get() == null) {
69 break;
70 }
71 }
72 }
73 }
74
75 class SimpleClassLoader extends ClassLoader {
76 private Hashtable classes = new Hashtable();
77
78 public SimpleClassLoader() {
79 }
|