CHAPTER 13 ■ GARBAGE COLLECTION
Summary
In this chapter, we've touched on one of Java's more advanced concepts: garbage collection. We've just
skimmed the surface of this advanced topic, with an eye toward providing you with the basic
information for optimizing your own applications. If you really get into optimization, you can find whole
books written about the subject.
In this brief overview, we covered:
- Memory allocation
- The garbage-collection algorithm used by Java
- The variations on the algorithm (naïve, incremental, and concurrent)
- How the G1 collector improves on the overall algorithm
- The difference between a scavenge and a full collection
- That garbage collection is event-driven
- That garbage collection is beyond our direct control, but can be controlled
indirectly - The most common settings that control garbage collection
- How to see what the garbage collector does in each of its runs
- How to use that information to optimize our garbage collection settings (and thus
the performance of our programs) - How to prevent an object from ever being garbage collected.
- That Java 7 offers an improved garbage collector named G1 (for “garbage first”)
For all that we've learned, I'd like to point out that you can do a lot of programming without ever
thinking about garbage collection. Other than to collect sample data for this chapter, I haven't changed a
single garbage collection setting for the programs in this book. However, if you stick with programming
in Java, your programs will eventually run into the bottleneck that arises from not controlling your
garbage collection settings. At that point, I want you to come back to this chapter and cover the basics.
Eventually, you may need more advanced information, but this set of basic information will let you solve
a lot of garbage collection problems.