* configure.ac: New switch for disabling -O2 (--disable-optimizations).
[cacao.git] / tests / gc / ClassUnload.java
1 import java.util.Vector;
2 import java.io.FileInputStream;
3
4 class ClassUnloadTest {
5         public static Vector classEater;
6
7         public static void classTest() {
8                 System.out.println("TC: Testclass eats up memory ...");
9                 classEater = new Vector();
10                 for (int i=0; i<1000000; i++)
11                         classEater.add(new String("I am a memory eater!"));
12
13                 System.out.println("TC: Testclass fine.");
14         }
15
16         static {
17                 System.out.println("TC: Static Initializer ...");
18         }
19 }
20
21 class ClassUnloadLoader extends ClassLoader {
22         public Vector myEater;
23
24         public ClassUnloadLoader() {
25                 super();
26                 System.out.println("CL: Initializer ...");
27         }
28
29         public Class myLoad() {
30                 Class c;
31
32                 System.out.println("CL: Classloader loads testing class ...");
33                 try {
34                         c = loadClass("ClassUnloadTest");
35                         ClassUnloadTest.classTest();
36                 } catch (Exception e) {
37                         System.out.println("EXCEPTION: " + e);
38                         c = null;
39                 }
40
41                 System.out.println("CL: Classloader eats up memory ...");
42                 myEater = new Vector();
43                 for (int i=0; i<1000000; i++)
44                         myEater.add(new String("I am a memory eater!"));
45
46                 return c;
47         }
48
49         public Class loadClass(String name) throws ClassNotFoundException {
50                 Class c;
51                 FileInputStream s;
52                 byte[] buf;
53                 int size;
54
55                 if (name != "ClassUnloadTest") {
56                         c = super.loadClass(name);
57                         return c;
58                 }
59
60                 System.out.println("CL: Now loading \'" + name + "\' ...");
61                 try {
62                         s = new FileInputStream(name + ".class");
63                         size = s.available();
64                         buf = new byte[size];
65                         s.read(buf);
66                         System.out.println("\tloaded a total of " + size + " bytes.");
67                         c = super.defineClass(name, buf, 0, size);
68                 } catch (Exception e) {
69                         System.out.println("EXCEPTION: " + e);
70                         throw new ClassNotFoundException();
71                 }
72
73                 return c;
74         }
75
76         protected void finalize() {
77                 System.out.println("CL: Classloader will be unloaded ...");
78         }
79 }
80
81 public class ClassUnload {
82         public static void printMemInfo() {
83                 Runtime r = Runtime.getRuntime();
84                 /*System.out.println("\tfree : " + r.freeMemory());*/
85                 /*System.out.println("\ttotal: " + r.totalMemory());*/
86                 /*System.out.println("\tmax  : " + r.maxMemory());*/
87                 System.out.println("\tused : " + (r.totalMemory() - r.freeMemory()));
88         }
89
90         public static void main(String[] s) {
91                 ClassUnloadLoader l;
92                 Class c;
93
94                 System.out.println("--: Running program ...");
95                 printMemInfo();
96
97                 System.out.println("--: Create classloader ...");
98                 l = new ClassUnloadLoader();
99
100                 System.out.println("--: Load stuff ...");
101                 c = l.myLoad();
102                 System.out.println("\tclass = " + c);
103
104                 printMemInfo();
105
106                 /*System.out.println("\tDEBUG l=" + l);*/
107                 /*System.out.println("\tDEBUG c=" + c);*/
108                 /*System.out.println("\tDEBUG c.loader=" + c.getClassLoader());*/
109
110                 /* REMEMBER: if you comment out one of the following two lines, no class unloading should happen! */
111                 l = null;
112                 c = null;
113
114                 System.out.println("--: Collect stuff ...");
115                 System.gc();
116                 System.gc();
117                 System.gc();
118
119                 printMemInfo();
120
121                 /*System.out.println("\tDEBUG l=" + l);*/
122                 /*System.out.println("\tDEBUG c=" + c);*/
123                 /*System.out.println("\tDEBUG c.loader=" + (c==null ? null : c.getClassLoader()));*/
124
125                 System.out.println("done.");
126         }
127 }