Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 import java.lang.ref.WeakReference;
     18 
     19 /**
     20  * Some finalizer tests.
     21  *
     22  * This only works if System.runFinalization() causes finalizers to run
     23  * immediately or very soon.
     24  */
     25 public class Main {
     26     private static void snooze(int ms) {
     27         try {
     28             Thread.sleep(ms);
     29         } catch (InterruptedException ie) {
     30             System.out.println("Snooze: " + ie.getMessage());
     31         }
     32     }
     33 
     34     public static WeakReference makeRef() {
     35         /*
     36          * Make ft in another thread, so there is no danger of
     37          * a conservative reference leaking onto the main thread's
     38          * stack.
     39          */
     40 
     41         final WeakReference[] wimp = new WeakReference[1];
     42         Thread t = new Thread() {
     43                 public void run() {
     44                     FinalizerTest ft = new FinalizerTest("wahoo");
     45                     wimp[0] = new WeakReference(ft);
     46                     ft = null;
     47                 }
     48             };
     49 
     50         t.start();
     51 
     52         try {
     53             t.join();
     54         } catch (InterruptedException ie) {
     55             throw new RuntimeException(ie);
     56         }
     57 
     58         return wimp[0];
     59     }
     60 
     61     public static String wimpString(final WeakReference wimp) {
     62         /*
     63          * Do the work in another thread, so there is no danger of a
     64          * conservative reference to ft leaking onto the main thread's
     65          * stack.
     66          */
     67 
     68         final String[] s = new String[1];
     69         Thread t = new Thread() {
     70                 public void run() {
     71                     Object ref = wimp.get();
     72                     if (ref != null) {
     73                         s[0] = ref.toString();
     74                     }
     75                 }
     76             };
     77 
     78         t.start();
     79 
     80         try {
     81             t.join();
     82         } catch (InterruptedException ie) {
     83             throw new RuntimeException(ie);
     84         }
     85 
     86         return s[0];
     87     }
     88 
     89     public static void main(String[] args) {
     90         WeakReference wimp = makeRef();
     91 
     92         System.out.println("wimp: " + wimpString(wimp));
     93 
     94         /* this will try to collect and finalize ft */
     95         System.out.println("gc");
     96         System.gc();
     97 
     98         System.out.println("wimp: " + wimpString(wimp));
     99         System.out.println("finalize");
    100         System.runFinalization();
    101         System.out.println("wimp: " + wimpString(wimp));
    102 
    103         System.out.println("sleep");
    104         snooze(1000);
    105 
    106         System.out.println("reborn: " + FinalizerTest.mReborn);
    107         System.out.println("wimp: " + wimpString(wimp));
    108         System.out.println("reset reborn");
    109         System.gc();
    110         FinalizerTest.mReborn = FinalizerTest.mNothing;
    111         System.out.println("gc + finalize");
    112         System.gc();
    113         System.runFinalization();
    114 
    115         System.out.println("sleep");
    116         snooze(1000);
    117 
    118         System.out.println("reborn: " + FinalizerTest.mReborn);
    119         System.out.println("wimp: " + wimpString(wimp));
    120     }
    121 }
    122