• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2008 The Android Open Source Project
2 
3 import java.lang.ref.WeakReference;
4 
5 /**
6  * Some finalizer tests.
7  *
8  * This only works if System.runFinalization() causes finalizers to run
9  * immediately or very soon.
10  */
11 public class Main {
snooze(int ms)12     private static void snooze(int ms) {
13         try {
14             Thread.sleep(ms);
15         } catch (InterruptedException ie) {
16             System.out.println("Snooze: " + ie.getMessage());
17         }
18     }
19 
makeRef()20     public static WeakReference makeRef() {
21         /*
22          * Make ft in another thread, so there is no danger of
23          * a conservative reference leaking onto the main thread's
24          * stack.
25          */
26 
27         final WeakReference[] wimp = new WeakReference[1];
28         Thread t = new Thread() {
29                 public void run() {
30                     FinalizerTest ft = new FinalizerTest("wahoo");
31                     wimp[0] = new WeakReference(ft);
32                     ft = null;
33                 }
34             };
35 
36         t.start();
37 
38         try {
39             t.join();
40         } catch (InterruptedException ie) {
41             throw new RuntimeException(ie);
42         }
43 
44         return wimp[0];
45     }
46 
wimpString(final WeakReference wimp)47     public static String wimpString(final WeakReference wimp) {
48         /*
49          * Do the work in another thread, so there is no danger of a
50          * conservative reference to ft leaking onto the main thread's
51          * stack.
52          */
53 
54         final String[] s = new String[1];
55         Thread t = new Thread() {
56                 public void run() {
57                     Object ref = wimp.get();
58                     if (ref != null) {
59                         s[0] = ref.toString();
60                     }
61                 }
62             };
63 
64         t.start();
65 
66         try {
67             t.join();
68         } catch (InterruptedException ie) {
69             throw new RuntimeException(ie);
70         }
71 
72         return s[0];
73     }
74 
main(String[] args)75     public static void main(String[] args) {
76         WeakReference wimp = makeRef();
77 
78         System.out.println("wimp: " + wimpString(wimp));
79 
80         /* this will try to collect and finalize ft */
81         System.out.println("gc");
82         System.gc();
83 
84         System.out.println("wimp: " + wimpString(wimp));
85         System.out.println("finalize");
86         System.runFinalization();
87         System.out.println("wimp: " + wimpString(wimp));
88 
89         System.out.println("sleep");
90         snooze(1000);
91 
92         System.out.println("reborn: " + FinalizerTest.mReborn);
93         System.out.println("wimp: " + wimpString(wimp));
94         System.out.println("reset reborn");
95         System.gc();
96         FinalizerTest.mReborn = FinalizerTest.mNothing;
97         System.out.println("gc + finalize");
98         System.gc();
99         System.runFinalization();
100 
101         System.out.println("sleep");
102         snooze(1000);
103 
104         System.out.println("reborn: " + FinalizerTest.mReborn);
105         System.out.println("wimp: " + wimpString(wimp));
106     }
107 }
108