Monday, July 6, 2009

This is how real men do garbage collection!

I was quite fascinated today seeing the way the famous JDK figure Martin Bucholtz does garbage collection. Anyone believing he takes garbage more seriously than that?

Enjoy:

private static final Runtime rt = Runtime.getRuntime();
static void gcTillYouDrop() {
rt.gc();
rt.runFinalization();
final CountDownLatch latch = new CountDownLatch(1);
new Object() {
protected void finalize() {
latch.countDown();
}
};
rt.gc();
try {
latch.await();
}
catch(InterruptedException ie){
throw new Error(ie);
}
}


"Hyper-paranoid" indeed, in the words of Kevin.

1 comment:

  1. What I found interesting is that I found this code in sun code benchmarks. (Google for CountdownLatch() gc() )

    But it makes a lot of sense in those situations where you want to make sure the garbage collector isn't interfering with the benchmark results.

    Thanks for this post!

    ReplyDelete