Home | History | Annotate | Download | only in ref
      1 /*
      2  * Copyright (C) 2011 Google Inc.
      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 package libcore.java.lang.ref;
     18 
     19 /**
     20  * Triggers finalizers to run. Note that live-precise bugs may interfere with
     21  * analysis of what is reachable. Unreachable references in local frames may not
     22  * be finalized even after a call to {@link #induceFinalization}. To work
     23  * around, create finalizable objects in helper methods. http://b/4191345
     24  */
     25 public final class FinalizationTester {
     26     private FinalizationTester() {}
     27 
     28     public static void induceFinalization() {
     29         // System.gc() does not garbage collect every time. Runtime.gc() is
     30         // more likely to perfom a gc.
     31         Runtime.getRuntime().gc();
     32         enqueueReferences();
     33         System.runFinalization();
     34     }
     35 
     36     public static void enqueueReferences() {
     37         /*
     38          * Hack. We don't have a programmatic way to wait for the reference queue
     39          * daemon to move references to the appropriate queues.
     40          */
     41         try {
     42             Thread.sleep(100);
     43         } catch (InterruptedException e) {
     44             throw new AssertionError();
     45         }
     46     }
     47 }
     48