Skip to main content

Garbage Collection

Posted by daniel on November 25, 2003 at 11:58 AM PST

Do you ever miss the bulldozer? You remember, in the early days, when it was time for garbage collection in a Java application everything would stop. To call out this advanced feature and to entertain you, you would see an animation of a bulldozer in action.

Brian Goetz's second article in his series on garbage collection leads off Also in Java Today . In his developerWorks article Garbage collection in the 1.4.1 JVM, Brian describes generational garbage collection. At its root, the idea is that objects either die very quickly or they hang around a long while. He explains that " Empirical studies have shown that for most object-oriented languages, the Java language included, the vast majority of objects -- as much as 98 percent, depending on your metric for object youth -- die young. One can measure an object's age in wall-clock seconds, in total bytes allocated by the memory management subsystem since the object was allocated, or the number of garbage collections since the object was allocated. But no matter how you measure, the studies show the same thing -- most objects die young."

Sounds a lot like the papers in my office. After a file or printout has been around for a week, it will be at least a month before I remember to come back and check whether I still need it or not. For kicks, find out about how people you know manage their email inbox and how large it is. You'll find a lot of interesting garbage collection strategies.

Brian points out that generational garbage collectors must track intergenerational references. In other words, " what if an object in the older generation references a younger object, which is not reachable through any other chain of references from a root?

To address this problem, generational collectors must explicitly track references from older objects to younger objects and add these old-to-young references into the root set of the minor collection. There are two ways to create a reference from an old object to a young object. Either one of the references contained in an old object is modified to refer to a young object, or a young object that refers to other young objects is promoted into the older generation."

The information contained in the article may not explicitly change the code that you write, but it is fun to take a look under the hood and try to understand some of what's going on. You can actually tune the garbage collector but Goetz says that "you'll probably get a better return on your tuning investment by making sure your application has been thoroughly profiled and optimized first."

While you are poking around your code, take time to consider whether you are employing Best Practices for Exception Handling. ONJava author Gunjan Doshi looks at exceptions that are caused by programming errors, client code errors, or resource failures. He writes that the only reason to create a checked exception is "If the client can take some alternate action to recover from the exception, make it a checked exception. If the client cannot do anything useful, then make the exception unchecked. By useful, I mean taking steps to recover from the exception and not just logging the exception."

Bill Day has always been a widely distributed application. So far that has required a lot of travel time. In today's Weblogs , Bill writes in his entry Answer Your MIDP 2.0 Questions that he is putting together a webinar. All you have to bring are your J2ME development questions after signing up for Developing Wireless Applications using MIDP 2.0, WMA, and MMA. Satya Komatineni follow's up on my post yesterday with his entry Architecture is language agnostic. There is so much to learn both from Java and C# environments. John Mitchell responds to Satya in Architecture: Abstract vs. Manifest. Finally, James Todd looks back at the JXTA Town Hall:2003.11.06 in his report.

We also link to a James Todd blog from the JXTA community in today's
Projects and Communities . James has debuted his JXTA Sightings which are the P2P analogue of Java Desktop's Swing Sightings. James points to a MSc project by Daniel Elenius which integrates JXTA and DAML-S. JXTA peers can " "search/use each others services (using JXTA-SOAP, and reasoning in JTP)." The java-net community features the metaframework project. The framework is built on the idea that "Coding Value Objects in XML is much more intuitive and quicker than coding Value Objects in Java. Likewise, assessing and manipulating data is easy."

In today's News Headlines :

Registered users can submit news items for the href=""> News Page using our
news submission form.
All submissions go through an editorial review by news director Steve
Mallet before being posted to the site. You can also subscribe to the href=""> News RSS

Current and upcoming Java Events:

Registered users can submit event listings for the href=""> Events Page using our href=""> events submission form.
All submissions go through an editorial review before being posted to the

This blog is delivered weekdays as the href="">Java Today RSS
feed. Once this page is no longer featured as the front page of href=""> it will be archived at href=""> You can
access other past issues in the href=""> Archive.