There are multiple garbage collectors in Java; but the popular one to study was the "eden" and aging model.
Of course, now that most people are running the G1 garbage collector, even that popular model is often not describing what is really taking place. Don't get too worried about the inaccuracy, for quite a few releases, it was the de-facto default.
Garbage collection is about two primary tasks, reclaiming memory and hole compaction.
- Reclaiming memory is done conceptually first. If the JVM cannot reach the object from a non-dead thread, then the object is reclaimed (as it cannot become part of the executing program in the future... the knowledge of the handle / address of the object has been permanently lost).
- Once objects have been reclaimed, they leave the heap in a "swiss cheese" state, with some regions being used, and other regions empty (due to reclaiming). Compaction attempts to remove the "holes" in the heap in such a way that attempts to allocate large objects will not fail because the memory requested is not available as a contiguous series of addresses.
With the old eden style garbage collectors, the ideas were that
- Newly created objects were at higher risk of being reclaimed because they probably were created within the scope of a block (and the references would probably be lost when exiting the block).
- Less-newly created objects were at lower risk of being reclaimed because they survived the block they were created in.
As such, the "eden" space was the part of the heap where an object wasn't yet checked to see if it was still reachable by the JVM program execution threads. Survivor space was where the object was copied into (copying allowed re-basing of the addresses and thus compaction), and other more permanent spaces indicated an even longer-lived object.
Now with the new G1 garbage collector, you effectively have thousands (to millions) of mini-heaps, and the entire heap is marked based on the survivability of the objects it contains. Compaction is occasionally done by combining two "heap blocks"; however, since the heaps are so much smaller, often they are just discarded instead of compacted (due to higher chance of all objects in a heap being unreachable when fewer objects are to be considered).