Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 16 | |
| 17 | import java.lang.ref.WeakReference; |
Brian Carlstrom | c849445 | 2014-02-20 13:50:05 -0800 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.List; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * Some finalizer tests. |
| 23 | * |
| 24 | * This only works if System.runFinalization() causes finalizers to run |
| 25 | * immediately or very soon. |
| 26 | */ |
| 27 | public class Main { |
| 28 | private static void snooze(int ms) { |
| 29 | try { |
| 30 | Thread.sleep(ms); |
| 31 | } catch (InterruptedException ie) { |
| 32 | System.out.println("Snooze: " + ie.getMessage()); |
| 33 | } |
| 34 | } |
| 35 | |
Brian Carlstrom | c849445 | 2014-02-20 13:50:05 -0800 | [diff] [blame] | 36 | public static WeakReference<FinalizerTest> makeRef() { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 37 | /* |
| 38 | * Make ft in another thread, so there is no danger of |
| 39 | * a conservative reference leaking onto the main thread's |
| 40 | * stack. |
| 41 | */ |
| 42 | |
Brian Carlstrom | c849445 | 2014-02-20 13:50:05 -0800 | [diff] [blame] | 43 | final List<WeakReference<FinalizerTest>> wimp = |
| 44 | new ArrayList<WeakReference<FinalizerTest>>(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 45 | Thread t = new Thread() { |
| 46 | public void run() { |
| 47 | FinalizerTest ft = new FinalizerTest("wahoo"); |
Brian Carlstrom | c849445 | 2014-02-20 13:50:05 -0800 | [diff] [blame] | 48 | wimp.add(new WeakReference<FinalizerTest>(ft)); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 49 | ft = null; |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | t.start(); |
| 54 | |
| 55 | try { |
| 56 | t.join(); |
| 57 | } catch (InterruptedException ie) { |
| 58 | throw new RuntimeException(ie); |
| 59 | } |
| 60 | |
Brian Carlstrom | c849445 | 2014-02-20 13:50:05 -0800 | [diff] [blame] | 61 | return wimp.get(0); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Brian Carlstrom | c849445 | 2014-02-20 13:50:05 -0800 | [diff] [blame] | 64 | public static String wimpString(final WeakReference<FinalizerTest> wimp) { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 65 | /* |
| 66 | * Do the work in another thread, so there is no danger of a |
| 67 | * conservative reference to ft leaking onto the main thread's |
| 68 | * stack. |
| 69 | */ |
| 70 | |
| 71 | final String[] s = new String[1]; |
| 72 | Thread t = new Thread() { |
| 73 | public void run() { |
Brian Carlstrom | c849445 | 2014-02-20 13:50:05 -0800 | [diff] [blame] | 74 | FinalizerTest ref = wimp.get(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 75 | if (ref != null) { |
| 76 | s[0] = ref.toString(); |
| 77 | } |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | t.start(); |
| 82 | |
| 83 | try { |
| 84 | t.join(); |
| 85 | } catch (InterruptedException ie) { |
| 86 | throw new RuntimeException(ie); |
| 87 | } |
| 88 | |
| 89 | return s[0]; |
| 90 | } |
| 91 | |
| 92 | public static void main(String[] args) { |
Brian Carlstrom | c849445 | 2014-02-20 13:50:05 -0800 | [diff] [blame] | 93 | WeakReference<FinalizerTest> wimp = makeRef(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 94 | |
| 95 | System.out.println("wimp: " + wimpString(wimp)); |
| 96 | |
| 97 | /* this will try to collect and finalize ft */ |
| 98 | System.out.println("gc"); |
Mathieu Chartier | 7befd0e | 2014-02-03 17:48:41 -0800 | [diff] [blame] | 99 | Runtime.getRuntime().gc(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 100 | |
| 101 | System.out.println("wimp: " + wimpString(wimp)); |
| 102 | System.out.println("finalize"); |
| 103 | System.runFinalization(); |
| 104 | System.out.println("wimp: " + wimpString(wimp)); |
| 105 | |
| 106 | System.out.println("sleep"); |
| 107 | snooze(1000); |
| 108 | |
| 109 | System.out.println("reborn: " + FinalizerTest.mReborn); |
| 110 | System.out.println("wimp: " + wimpString(wimp)); |
| 111 | System.out.println("reset reborn"); |
Mathieu Chartier | 7befd0e | 2014-02-03 17:48:41 -0800 | [diff] [blame] | 112 | Runtime.getRuntime().gc(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 113 | FinalizerTest.mReborn = FinalizerTest.mNothing; |
| 114 | System.out.println("gc + finalize"); |
| 115 | System.gc(); |
| 116 | System.runFinalization(); |
| 117 | |
| 118 | System.out.println("sleep"); |
| 119 | snooze(1000); |
| 120 | |
| 121 | System.out.println("reborn: " + FinalizerTest.mReborn); |
| 122 | System.out.println("wimp: " + wimpString(wimp)); |
Mathieu Chartier | 3a5fa5e | 2014-09-04 14:15:35 -0700 | [diff] [blame] | 123 | // Test runFinalization with multiple objects. |
| 124 | runFinalizationTest(); |
| 125 | } |
| 126 | |
| 127 | static class FinalizeCounter { |
Mathieu Chartier | bbb5479 | 2014-10-15 09:59:03 -0700 | [diff] [blame^] | 128 | public static final int maxCount = 1024; |
| 129 | public static boolean finalized[] = new boolean[maxCount]; |
Mathieu Chartier | 3a5fa5e | 2014-09-04 14:15:35 -0700 | [diff] [blame] | 130 | private static Object finalizeLock = new Object(); |
| 131 | private static volatile int finalizeCount = 0; |
| 132 | private int index; |
| 133 | static int getCount() { |
| 134 | return finalizeCount; |
| 135 | } |
Mathieu Chartier | bbb5479 | 2014-10-15 09:59:03 -0700 | [diff] [blame^] | 136 | static void printNonFinalized() { |
| 137 | for (int i = 0; i < maxCount; ++i) { |
| 138 | if (!FinalizeCounter.finalized[i]) { |
| 139 | System.err.println("Element " + i + " was not finalized"); |
| 140 | } |
| 141 | } |
| 142 | } |
Mathieu Chartier | 3a5fa5e | 2014-09-04 14:15:35 -0700 | [diff] [blame] | 143 | FinalizeCounter(int index) { |
| 144 | this.index = index; |
| 145 | } |
| 146 | protected void finalize() { |
| 147 | synchronized(finalizeLock) { |
| 148 | ++finalizeCount; |
Mathieu Chartier | bbb5479 | 2014-10-15 09:59:03 -0700 | [diff] [blame^] | 149 | finalized[index] = true; |
Mathieu Chartier | 3a5fa5e | 2014-09-04 14:15:35 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Mathieu Chartier | 8cb0306 | 2014-10-13 10:58:01 -0700 | [diff] [blame] | 154 | private static void allocFinalizableObjects(int count) { |
Mathieu Chartier | 3a5fa5e | 2014-09-04 14:15:35 -0700 | [diff] [blame] | 155 | Object[] objs = new Object[count]; |
| 156 | for (int i = 0; i < count; ++i) { |
| 157 | objs[i] = new FinalizeCounter(i); |
| 158 | } |
Mathieu Chartier | 8cb0306 | 2014-10-13 10:58:01 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | private static void runFinalizationTest() { |
Mathieu Chartier | bbb5479 | 2014-10-15 09:59:03 -0700 | [diff] [blame^] | 162 | allocFinalizableObjects(FinalizeCounter.maxCount); |
Mathieu Chartier | 8cb0306 | 2014-10-13 10:58:01 -0700 | [diff] [blame] | 163 | Runtime.getRuntime().gc(); |
Mathieu Chartier | 3a5fa5e | 2014-09-04 14:15:35 -0700 | [diff] [blame] | 164 | System.runFinalization(); |
Mathieu Chartier | bbb5479 | 2014-10-15 09:59:03 -0700 | [diff] [blame^] | 165 | System.out.println("Finalized " + FinalizeCounter.getCount() + " / " + FinalizeCounter.maxCount); |
| 166 | if (FinalizeCounter.getCount() != FinalizeCounter.maxCount) { |
| 167 | // Print out all the finalized elements. |
| 168 | FinalizeCounter.printNonFinalized(); |
| 169 | // Try to sleep for a couple seconds to see if the objects became finalized after. |
| 170 | try { |
| 171 | java.lang.Thread.sleep(2000); |
| 172 | } catch (InterruptedException e) { |
| 173 | } |
| 174 | System.out.println("After sleep finalized " + FinalizeCounter.getCount() + " / " + FinalizeCounter.maxCount); |
| 175 | FinalizeCounter.printNonFinalized(); |
| 176 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 177 | } |
Brian Carlstrom | c849445 | 2014-02-20 13:50:05 -0800 | [diff] [blame] | 178 | |
| 179 | public static class FinalizerTest { |
| 180 | public static FinalizerTest mNothing = new FinalizerTest("nothing"); |
| 181 | public static FinalizerTest mReborn = mNothing; |
| 182 | |
| 183 | private final String message; |
| 184 | private boolean finalized = false; |
| 185 | |
| 186 | public FinalizerTest(String message) { |
| 187 | this.message = message; |
| 188 | } |
| 189 | |
| 190 | public String toString() { |
| 191 | return "[FinalizerTest message=" + message + |
| 192 | ", finalized=" + finalized + "]"; |
| 193 | } |
| 194 | |
| 195 | protected void finalize() { |
| 196 | finalized = true; |
| 197 | mReborn = this; |
| 198 | } |
| 199 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 200 | } |