1 import java.util.Vector;
2 import java.io.FileInputStream;
4 class ClassUnloadTest {
5 public static Vector classEater;
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!"));
13 System.out.println("TC: Testclass fine.");
17 System.out.println("TC: Static Initializer ...");
21 class ClassUnloadLoader extends ClassLoader {
22 public Vector myEater;
24 public ClassUnloadLoader() {
26 System.out.println("CL: Initializer ...");
29 public Class myLoad() {
32 System.out.println("CL: Classloader loads testing class ...");
34 c = loadClass("ClassUnloadTest");
35 ClassUnloadTest.classTest();
36 } catch (Exception e) {
37 System.out.println("EXCEPTION: " + e);
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!"));
49 public Class loadClass(String name) throws ClassNotFoundException {
55 if (name != "ClassUnloadTest") {
56 c = super.loadClass(name);
60 System.out.println("CL: Now loading \'" + name + "\' ...");
62 s = new FileInputStream(name + ".class");
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();
76 protected void finalize() {
77 System.out.println("CL: Classloader will be unloaded ...");
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()));
90 public static void main(String[] s) {
94 System.out.println("--: Running program ...");
97 System.out.println("--: Create classloader ...");
98 l = new ClassUnloadLoader();
100 System.out.println("--: Load stuff ...");
102 System.out.println("\tclass = " + c);
106 /*System.out.println("\tDEBUG l=" + l);*/
107 /*System.out.println("\tDEBUG c=" + c);*/
108 /*System.out.println("\tDEBUG c.loader=" + c.getClassLoader());*/
110 /* REMEMBER: if you comment out one of the following two lines, no class unloading should happen! */
114 System.out.println("--: Collect stuff ...");
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()));*/
125 System.out.println("done.");