| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.util; |
| 18 |
|
| 19 |
import android.util.Slog; |
| 20 |
|
| 21 |
import java.util.concurrent.CountDownLatch; |
| 22 |
import java.util.concurrent.TimeUnit; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public final class GcUtils { |
| 30 |
private static final String TAG = GcUtils.class.getSimpleName(); |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public static void runGcAndFinalizersSync() { |
| 36 |
Runtime.getRuntime().gc(); |
| 37 |
Runtime.getRuntime().runFinalization(); |
| 38 |
|
| 39 |
final CountDownLatch fence = new CountDownLatch(1); |
| 40 |
createFinalizationObserver(fence); |
| 41 |
try { |
| 42 |
do { |
| 43 |
Runtime.getRuntime().gc(); |
| 44 |
Runtime.getRuntime().runFinalization(); |
| 45 |
} while (!fence.await(100, TimeUnit.MILLISECONDS)); |
| 46 |
} catch (InterruptedException ex) { |
| 47 |
throw new RuntimeException(ex); |
| 48 |
} |
| 49 |
Slog.v(TAG, "Running gc and finalizers"); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private static void createFinalizationObserver(CountDownLatch fence) { |
| 58 |
new Object() { |
| 59 |
@Override |
| 60 |
protected void finalize() throws Throwable { |
| 61 |
try { |
| 62 |
fence.countDown(); |
| 63 |
} finally { |
| 64 |
super.finalize(); |
| 65 |
} |
| 66 |
} |
| 67 |
}; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
private GcUtils() {} |
| 72 |
} |