xref: /aosp_15_r20/art/test/1336-short-finalizer-timeout/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2007 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 dalvik.system.VMRuntime;
18 import java.util.concurrent.CountDownLatch;
19 import static java.util.concurrent.TimeUnit.MINUTES;
20 
21 /**
22  * Test a class with a bad finalizer in an environment with a short finalizer timeout.
23  *
24  * This test is inherently flaky. It assumes that the system will schedule the finalizer daemon
25  * and finalizer watchdog daemon soon and often enough to reach the timeout and throw the fatal
26  * exception before we time out here.
27  * Largely cloned from 030-bad-finalizer.
28  */
29 public class Main {
main(String[] args)30     public static void main(String[] args) throws Exception {
31         CountDownLatch finalizerWait = new CountDownLatch(1);
32 
33         System.out.println("Finalizer timeout = "
34                 + VMRuntime.getRuntime().getFinalizerTimeoutMs() + " msecs.");
35         // A separate method to ensure no dex register keeps the object alive.
36         createBadFinalizer(finalizerWait);
37 
38         for (int i = 0; i < 2; i++) {
39             Runtime.getRuntime().gc();
40         }
41 
42         // Now wait for the finalizer to start running.
43         if (!finalizerWait.await(1, MINUTES)) {
44             System.out.println("finalizerWait timed out.");
45         }
46 
47         // Sleep for less time than the default finalizer timeout, but significantly more than
48         // the new timeout plus the 5 seconds we wait to dump thread stacks before actually
49         // exiting.
50         snooze(9800);
51 
52         // We should not get here, since it should only take 5.5 seconds for the timed out
53         // finalizer to kill the process.
54         System.out.println("UNREACHABLE");
55         System.exit(0);
56     }
57 
createBadFinalizer(CountDownLatch finalizerWait)58     private static void createBadFinalizer(CountDownLatch finalizerWait) {
59         BadFinalizer bf = new BadFinalizer(finalizerWait);
60 
61         System.out.println("About to null reference.");
62         bf = null;  // Not that this would make a difference, could be eliminated earlier.
63     }
64 
snooze(int ms)65     public static void snooze(int ms) {
66         try {
67             Thread.sleep(ms);
68         } catch (InterruptedException ie) {
69             System.out.println("Snooze(" + ms + ") interrupted");
70         }
71     }
72 
73     /**
74      * Class with a bad finalizer.
75      */
76     public static class BadFinalizer {
77         private CountDownLatch finalizerWait;
78         private volatile int j = 0;  // Volatile in an effort to curb loop optimization.
79 
BadFinalizer(CountDownLatch finalizerWait)80         public BadFinalizer(CountDownLatch finalizerWait) {
81             this.finalizerWait = finalizerWait;
82         }
83 
finalize()84         protected void finalize() {
85             System.out.println("Finalizer started and snoozing...");
86             finalizerWait.countDown();
87             snooze(200);
88             System.out.println("Finalizer done snoozing.");
89 
90             System.out.println("Finalizer sleeping forever now.");
91             while (true) {
92                 snooze(10000);
93             }
94         }
95     }
96 }
97