Notes on Java: Garbage Collection

One of the advantages that proponents of Java love about the language is garbage collection — the process in which JVM cleans up after your program is done partying and messing with your computer’s memory — free of charge. Th-unk you. Com’ again. Buh-bye.

Simplistically speaking, in comparison, C and C programmers typically need to clean up after themselves. The problem with that is, sometimes these parties get so wild that it makes the clean up afterwards impossible. That’s when “memory leaks” occur (ahem, Safari). The “parties”, of course, are the programs that rampage through your computer’s memory when they are having the time of their lives (which is not all that long). I mean, WHO likes cleaning up after parties, right? Think about it: Even if you try, there are bound to be spots where you just can’t get to or miss out from all the other cleaning you have to do YOURSELF. Java frees programmers from that headache by being in the business of memory salvation. Bless Java.

So here’s how it works… Whenever your Java program disposes something that it no longer cares for in your system memory, it’s considered trash, garbage, litter, junk, scrap, rubbish, Microsoft’s customers, whatever name suits your fancy, and is eligible for Java’s free garbage collection service. Did I mention enrollment is automatic, and it’s free? With small parties, Java only cleans up after your mess once in a while. But with parties in the size of Coldplay’s concerts, Java will do it a bit more frequently.

Well, I lied when I said it’s free. Com’on, nothing is free. You know that. Luckily, it costs in terms of system performance with all that bookkeeping and cleaning Java has to do. But it’s not really that bad if you consider the work Java’s done for you and the time and brain cells it saves you. I may be going out on a limb here, but I’d say the small performance hit your program suffers is well worth the price. And besides, with today’s qua-gazillion-megahertz computing power and memory availability, these performance hits are barely noticeable. It’s like being thrown at with a penny — of course it hurts. But just a little, and the pain only lasts long enough for you to realize that the sucker who threw the penny is now a penny poorer, and you are now a penny richer…

On a different note, the new Mac OSX Leopard is supposed to ship with a new version of Xcode that comes with garbage collection feature… Buh-bye, major memory leaks…