among software developers. If such a call is made, we will also display a message, since writ-
ing state in the finalizer shows that some change isn't being recorded in the main body of the
program. This is a lurking bug that we will want to fix.
Also note that the exception handling in the writeState() method is completely inadequate.
If this were anything other than an example in a book, I would have to do something very
different to make sure that the new state is actually saved. But since it is just an example in a
book, I will merely apologize.
The moral of this story is, at bottom, that we should not be greedy. Garbage collection is one
of the good things about Java, and it is a good thing that makes our programs more reliable
without requiring us to do all the hard thinking about memory management that we had to do
with earlier languages. But we shouldn't mistake the help Java gives us in the area of memory
management as a license to forget about all of the scarce resources that our program might be
using. Although we can tie the management of such resources to the garbage collector, doing
so is probably a bad idea. Instead, we can use those ties as a final check. But we shouldn't use
it for more than that, or depend on the garbage collector to do our work for us.
[ 14 ] It must be stressed that this simplicity is only for the user of garbage collection. If you are trying to
implement garbage collection, things are devilishly hard, which is one of the reasons that garbage col-
lection took so long to become part of widely used languages.
[ 15 ] Before my mailbox is filled with angry email from former Lisp or Smalltalk programmers, let me
hasten to add that Java was not the first language to have garbage collection, or even the first system
language that had garbage collection. Like most of the other features of Java, garbage collection was
well known in other languages first; that is one of the reasons that it was added to Java.
tp://linuxdevcenter.com/pub/a/linux/2003/05/08/cpp_mm-1.html ) , or Venu Dasigi, “A Catalog of Com-
mon Bugs in C++ Programming” ( http://cs.spsu.edu/vdasigi/Debugging/CCSC99pap.pdf ).