1/*
2 * Copyright (c) 2003, 2007, 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 4809008
27 * @build Bean
28 * @run main/othervm -server -XX:SoftRefLRUPolicyMSPerMB=0 Test4809008
29 * @summary Tests memory leak with multiple class loader access
30 * @author Mark Davidson
31 */
32
33import java.beans.BeanInfo;
34import java.beans.Introspector;
35
36/**
37 * This tests to see if the classes will be garbage collected when multiple
38 * short lived classloaders populate the BeanInfo cache with classes.
39 * <p/>
40 * We are also trying to verify if the ClassLoader finalize method is called.
41 * <p/>
42 * Use:
43 * java -verbose:class      to print out class loading.
44 * java -verbose:gc         to print out gc events.
45 */
46public class Test4809008 {
47    public static void main(String[] args) throws Exception {
48        printMemory("Start Memory");
49        int introspected = 200;
50        for (int i = 0; i < introspected; i++) {
51            ClassLoader cl = new SimpleClassLoader();
52            Class type = cl.loadClass("Bean");
53            type.newInstance();
54
55            // The methods and the bean info should be cached
56            BeanInfo info = Introspector.getBeanInfo(type);
57
58            cl = null;
59            type = null;
60            info = null;
61            System.gc();
62        }
63        System.runFinalization();
64        printMemory("End Memory");
65
66        int finalized = SimpleClassLoader.numFinalizers;
67        System.out.println(introspected + " classes introspected");
68        System.out.println(finalized + " classes finalized");
69
70        // good if at least half of the finalizers are run
71        if (finalized < (introspected >> 1)) {
72            throw new Error("ClassLoaders not finalized: " + finalized);
73        }
74    }
75
76    private static void printMemory(String message) {
77        Runtime runtime = Runtime.getRuntime();
78        runtime.gc();
79        long free = runtime.freeMemory();
80        long total = runtime.totalMemory();
81        System.out.println(message);
82        System.out.println("\tfree:  " + free);
83        System.out.println("\ttotal: " + total);
84    }
85}
86