Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

J2SE 1.4.1 boosts garbage collection

Three new algorithms target near real-time applications

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Prior to the introduction of Java 2 Platform, Standard Edition (J2SE) 1.4.1, writing effective applications that needed high throughput or minimal pause time during execution proved difficult for Java developers. Now, version 1.4.1's three new garbage collection algorithms specifically target these types of applications. In this article, I introduce these algorithms in the broader context of Java application development. I discuss the advantages and disadvantages of the algorithms, and recommend when they should and should not be used.

A useful metaphor

Imagine you live in a really small town (let's call it JavaVille). It has one main street, and, for fun, residents cruise the street, throwing candy wrappers and old computer parts out their car windows. This creates quite a mess, and the town has hired a full-time garbage collector to deal with the problem.

Bear with me for a minute. This is a really useful metaphor.

Now, imagine you are the town garbage collector, or sanitation engineer. With your garbage truck, you pick up all the trash left on the street. However, your truck is so big that no other cars can travel on the main street while you're collecting garbage. This gives people something else to complain about—they can't do anything else while the garbage truck travels through town. You try to stay out of the way, but sometimes, certain vehicles must get through, like ambulances and fire trucks.

To alleviate the situation, the town council votes to add some new streets so people can go about their business even when the garbage truck is running. They also decide to buy more garbage trucks so garbage collection takes less time.

This is a really useful metaphor for thinking about Java garbage collection, where multiple application threads (litterbugs) create garbage, and one thread (you) cleans it up. The application threads and the garbage collector share a single CPU (the main street), and all activity in the applications must stop while garbage collection is in progress (stop the world). You can make garbage collection less noticeable by adding more CPUs (streets) or more garbage collection (GC) threads (garbage trucks). Note that adding more GC threads (trucks) won't do you any good unless you have enough CPUs (streets) for them to work effectively in parallel.

Garbage collection is a complex subject. Before I discuss the new garbage collection algorithms, we should look at the history of Java garbage collection and the situation as it existed before J2SE 1.4.1. This should make it easier to understand current garbage collection and the reasons behind the new algorithms.

Historical background

When Java was originally developed, the JDK shipped with a mark-and-sweep garbage collector. A mark-and-sweep garbage collector proceeds in two phases:

  1. Mark: identifies garbage objects
  2. Sweep: reclaims the memory for the garbage objects


Garbage objects are identified by traversing references from the current application stack frames; unreachable objects are assumed to be garbage.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (2)
Login
Forgot your account info?

Metaphor chosen was well-thought & helpful to understand...By Anonymous on April 29, 2009, 7:56 amMetaphor chosen was well-thought & helpful to understand. Thanks for this nice article. Ananth

Reply | Read entire comment

thanks so muchBy Anonymous on January 23, 2009, 4:02 pmthanks so much

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources