CHAPTER 13 ■ GARBAGE COLLECTION
Consider the lines in bold (the targets array and the lines within the paint method that create new
instances of the Target object). If you visualize the values of all of the objects and primitives as the
program runs, you can see that the paint method first creates five instances of the Target class and then
keeps five of the instances going as the existing instances declare themselves to be finished.
Consequently, the result of creating an instance of the TargetClickPanel also creates five instances of the
Target class.
However, creating an instance of the TargetClickPanel class actually creates many more than just
five instances of the Target class. The instances of the Target class that have marked themselves as
finished still exist, even though the instance of the TargetClickPanel no longer has references to them.
That’s the key to garbage collection: references. We'll get to that shortly. First, let's consider the state of
the Target objects that have been created after the TargetClick game has run for a few seconds:
Target 1: Created because targets[0] was null. Now finished.
Target 2: Created because targets[1] was null. Now finished.
Target 3: Created because targets[2] was null. Now finished.
Target 4: Created because targets[3] was null. Now finished.
Target 5: Created because targets[4] was null. Now finished.
Target 6: Created because targets[0] was finished. Now finished.
Target 7: Created because targets[1] was finished. Now finished.
Target 8: Created because targets[2] was finished. Still in play.
Target 9: Created because targets[3] was finished. Still in play.
Target 10: Created because targets[4] was finished. Still in play.
Target 11: Created because targets[0] was finished. Still in play.
Target 12: Created because targets[1] was finished. Still in play.
And so on...
At any given moment, the instance of the TargetClickPanel class has references to five instances of
the Target class. But what happens to all the finished instances? As it happens, they get garbage
collected. And there's the primary rule of garbage collection: If an object no longer has references held
on it by other objects, that object gets garbage collected. In other words, once an object no longer has
any other objects using it, that object is ready to be removed from memory. Yet another way to say this is
that the object is now unreachable, meaning that no other bit of code can reach that object.
The Java Garbage Collection Algorithm: Marking and Sweeping
The algorithm that Java uses for garbage collection is called marking and sweeping. Conceptually, it's a
simple idea. First, make an initial pass of the memory and mark any memory that can be collected. Then
make another pass and free the memory that was marked in the first pass.
In practice, though, the process becomes much more complex. The basic algorithm (often called
naïve mark and sweep) works fine, so long as the whole program stops while the garbage collector runs.
The program has to stop so that the garbage collector can be sure that no new objects are being created
and that no existing objects have gotten into a state such that they can be garbage collected while the
collector runs. Java's earliest versions used just such an algorithm. Consequently, Java earned a
reputation for being unsuitable for applications that required high performance, from video games to