* configure.ac: New switch for disabling -O2 (--disable-optimizations).
[cacao.git] / tests / gc / Final.java
1 import java.util.Vector;
2 import java.util.Enumeration;
3
4 public class Final {
5         public static final int N=20;
6         public static final int SLEEP=1000;
7         public static final int MOD_REMEMBERED =3;
8         public static final int MOD_RESURRECTED=4;
9         public static final int MOD_UGLY       =10;
10
11         public static Vector<Final> resurrected;
12         public static Vector<Final> remembered;
13
14         public int id;
15         
16         protected void finalize() throws Throwable {
17                 System.out.println("\tFinalized object #" + id);
18
19                 if (id % MOD_RESURRECTED == 0) {
20                         System.out.println("\tFinalizer #" + id + " resurrected object");
21                         resurrected.add(this);
22                 }
23
24                 if (id % MOD_UGLY == 0) {
25                         System.out.println("\tFinalizer #" + id + " does an ugly thing!");
26                         System.gc();
27                 }
28
29                 super.finalize();
30         }
31
32         public static void main(String args[]) {
33                 Enumeration<Final> en;
34                 Final f = null;
35
36                 resurrected = new Vector<Final>(N);
37                 remembered  = new Vector<Final>(N);
38
39                 System.out.println("Creating objects ...");
40                 for (int i=0; i<N; i++) {
41                         f = new Final();
42                         f.id = i;
43
44                         if (i % MOD_REMEMBERED == 0) {
45                                 System.out.println("\tRemembering object #" + i);
46                                 remembered.add(f);
47                         }
48
49                 }
50                 f = null;
51
52                 System.out.println("Forcing collection 1 ...");
53                 System.gc();
54                 System.out.println("Forcing collection 2 ...");
55                 System.gc();
56                 System.out.println("Forcing collection 3 ...");
57                 System.gc();
58
59                 for (long i=0; i<400000000; i++);
60                 /*try {
61                         Thread.sleep(SLEEP);
62                 } catch (Exception e) {
63                 }*/
64
65                 en = remembered.elements();
66                 while (en.hasMoreElements()) {
67                         f = en.nextElement();
68                         System.out.println("Preserved object #" + f.id);
69                 }
70
71                 en = resurrected.elements();
72                 while (en.hasMoreElements()) {
73                         f = en.nextElement();
74                         System.out.println("Resurrected object #" + f.id);
75                 }
76
77                 f = null;
78                 remembered = null;
79                 resurrected = null;
80
81                 System.out.println("Forcing collection 4 ...");
82                 System.gc();
83                 System.out.println("Forcing collection 5 ...");
84                 System.gc();
85
86                 for (long i=0; i<400000000; i++);
87                 /*try {
88                         Thread.sleep(SLEEP);
89                 } catch (Exception e) {
90                 }*/
91
92                 System.out.println("Shutting down ...");
93                 System.exit(0);
94         }
95 }