pools. As the application runs, memory pools are resized as needed. These resizes
are performed by the GC subsystem.
Objects in the Heap
Objects are created in Eden by application threads, and are removed by a nondeter‐
ministic garbage collection cycle. The GC cycle runs when necessary (i.e., when
memory is getting low). The heap is divided into two generations, young and old.
The young generation comprises three spaces, Eden and two survivor spaces,
whereas the old generation has just one memory space
After surviving several GC cycles, objects get promoted to the old generation. Col‐
lections that only collect the young generation are usually very cheap (in terms of
computation required). HotSpot uses a more advanced form of mark and sweep
than we have seen so far, and is prepared to do extra bookkeeping to improve GC
performance. In the next section, let's move on to discuss the old generation and
how HotSpot handles longer-lived objects.
Collecting the Old Generation
When discussing garbage collectors, there is one other important piece of terminol‐
ogy that developers should know:
A garbage collector that uses multiple threads to perform collection
A garbage collector that can run at the same time as application threads are still
All the collectors we have met up until now are parallel, but not concurrent, collec‐
tors. By default, the collector for the old generation is also a parallel (but not con‐
current) mark and sweep collector, but HotSpot allows different collectors to be
plugged in. For example, later on in this section we'll meet CMS, which is a parallel
and mostly concurrent collector that ships with HotSpot.
Returning to the default collector, it seems at first glance to be similar to the collec‐
tor used for the young generation. However, it differs in one very important respect
—it is not an evacuating collector. Instead, the old generation is compacted when
collection occurs. This is important so that the memory space does not become
fragmented over the course of time.
This section is completely HotSpot-specific, and a detailed treatment is outside the
scope of the topic, but it is worth knowing about the existence of alternate collec‐
tors. For non-HotSpot users, you should consult your JVM's documentation to see
what options may be available for you.