| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import android.system.SystemCleaner; |
| 18 |
import dalvik.system.VMRuntime; |
| 19 |
import java.util.concurrent.CountDownLatch; |
| 20 |
|
| 21 |
import static java.util.concurrent.TimeUnit.MINUTES; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class Main { |
| 32 |
public static void main(String[] args) throws Exception { |
| 33 |
CountDownLatch cleanerWait = new CountDownLatch(1); |
| 34 |
|
| 35 |
registerBadCleaner(cleanerWait); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
for (int i = 0; i < 5; i++) { |
| 40 |
Runtime.getRuntime().gc(); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
cleanerWait.await(1, MINUTES); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
final long timeout = 100 * 1000 + VMRuntime.getRuntime().getFinalizerTimeoutMs(); |
| 53 |
long remainingWait = timeout; |
| 54 |
final long waitStart = System.currentTimeMillis(); |
| 55 |
while (remainingWait > 0) { |
| 56 |
synchronized (args) { |
| 57 |
try { |
| 58 |
args.wait(remainingWait); |
| 59 |
} catch (Exception e) { |
| 60 |
System.out.println("UNEXPECTED EXCEPTION"); |
| 61 |
} |
| 62 |
} |
| 63 |
remainingWait = timeout - (System.currentTimeMillis() - waitStart); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
System.out.println("UNREACHABLE"); |
| 68 |
System.exit(0); |
| 69 |
} |
| 70 |
|
| 71 |
private static void registerBadCleaner(CountDownLatch cleanerWait) { |
| 72 |
Object obj = new Object(); |
| 73 |
SystemCleaner.cleaner().register(obj, () -> neverEndingCleanup(cleanerWait)); |
| 74 |
|
| 75 |
System.out.println("About to null reference."); |
| 76 |
obj = null; |
| 77 |
} |
| 78 |
|
| 79 |
public static void snooze(int ms) { |
| 80 |
try { |
| 81 |
Thread.sleep(ms); |
| 82 |
} catch (InterruptedException ie) { |
| 83 |
System.out.println("Unexpected interrupt"); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
private static void neverEndingCleanup(CountDownLatch cleanerWait) { |
| 88 |
cleanerWait.countDown(); |
| 89 |
|
| 90 |
System.out.println("Cleaner started and sleeping briefly..."); |
| 91 |
|
| 92 |
long start, end; |
| 93 |
start = System.nanoTime(); |
| 94 |
snooze(2000); |
| 95 |
end = System.nanoTime(); |
| 96 |
System.out.println("Cleaner done snoozing."); |
| 97 |
|
| 98 |
System.out.println("Cleaner sleeping forever now."); |
| 99 |
while (true) { |
| 100 |
snooze(10000); |
| 101 |
} |
| 102 |
} |
| 103 |
} |