|
G1 is a generational, incremental, parallel, mostly concurrent, stop-the-world, and evacuating garbage collector which monitors pause-time goals in each of the stop-the-world pauses. Similar to other collectors, G1 splits the heap into (virtual) young and old generations. Space-reclamation efforts concentrate on the young generation where it is most efficient to do so, with occasional space-reclamation in the old generation Some operations are always performed in stop-the-world pauses to improve throughput. Other operations that would take more time with the application stopped such as whole-heap operations like global marking are performed in parallel and concurrently with the application. To keep stop-the-world pauses short for space-reclamation, G1 performs space-reclamation incrementally in steps and in parallel. G1 achieves predictability by tracking information about previous application behavior and garbage collection pauses to build a model of the associated costs. It uses this information to size the work done in the pauses. For example, G1 reclaims space in the most efficient areas first (that is the areas that are mostly filled with garbage, therefore the name). G1 reclaims space mostly by using evacuation: live objects found within selected memory areas to collect are copied into new memory areas, compacting them in the process. After an evacuation has been completed, the space previously occupied by live objects is reused for allocation by the application. The Garbage-First collector is not a real-time collector. It tries to meet set pause-time targets with high probability over a longer time, but not always with absolute certainty for a given pause. Heap Layout
G1 partitions the heap into a set of equally sized heap regions, each a contiguous range of virtual memory as shown in Figure 9-1. A region is the unit of memory allocation and memory reclamation. At any given time, each of these regions can be empty (light gray), or assigned to a particular generation, young or old. As requests for memory comes in, the memory manager hands out free regions. The memory manager assigns them to a generation and then returns them to the application as free space into which it can allocate itself.
Figure 7-1 G1 Garbage Collector Heap Layout
Description of "Figure 7-1 G1 Garbage Collector Heap Layout " The young generation contains eden regions (red) and survivor regions (red with "S"). These regions provide the same function as the respective contiguous spaces in other collectors, with the difference that in G1 these regions are typically laid out in a noncontiguous pattern in memory. Old regions (light blue) make up the old generation. Old generation regions may be humongous (light blue with "H") for objects that span multiple regions. An application always allocates into a young generation, that is, eden regions, with the exception of humongous objects that are directly allocated as belonging to the old generation. G1 garbage collection pauses can reclaim space in the young generation as a whole, and any additional set of old generation regions at any collection pause. During the pause G1 copies objects from this collection set to one or more different regions in the heap. The destination region for an object depends on the source region of that object: the entire young generation is copied into either survivor or old regions, and objects from old regions to other, different old regions using aging. Garbage Collection Cycle
On a high level, the G1 collector alternates between two phases. The young-only phase contains garbage collections that fill up the currently available memory with objects in the old generation gradually. The space-reclamation phase is where G1 reclaims space in the old generation incrementally, in addition to handling the young generation. Then the cycle restarts with a young-only phase. Figure 9-2 gives an overview about this cycle with an example of the sequence of garbage collection pauses that could occur:
Figure 7-2 Garbage Collection Cycle Overview
Description of "Figure 7-2 Garbage Collection Cycle Overview " The following list describes the phases, their pauses and the transition between the phases of the G1 garbage collection cycle in detail:
Young-only phase: This phase starts with a few Normal young collections that promote objects into the old generation. The transition between the young-only phase and the space-reclamation phase starts when the old generation occupancy reaches a certain threshold, the Initiating Heap Occupancy threshold. At this time, G1 schedules a Concurrent Start young collection instead of a Normal young collection.
Concurrent Start : This type of collection starts the marking process in addition to performing a Normal young collection. Concurrent marking determines all currently reachable (live) objects in the old generation regions to be kept for the following space-reclamation phase. While collection marking hasn’t completely finished, Normal young collections may occur. Marking finishes with two special stop-the-world pauses: Remark and Cleanup.
Remark: This pause finalizes the marking itself, performs global reference processing and class unloading, reclaims completely empty regions and cleans up internal data structures. Between Remark and Cleanup G1 calculates information to later be able to reclaim free space in selected old generation regions concurrently, which will be finalized in the Cleanup pause.
Cleanup: This pause determines whether a space-reclamation phase will actually follow. If a space-reclamation phase follows, the young-only phase completes with a single Prepare Mixed young collection.
Space-reclamation phase: This phase consists of multiple Mixed collections that in addition to young generation regions, also evacuate live objects of sets of old generation regions. The space-reclamation phase ends when G1 determines that evacuating more old generation regions wouldn't yield enough free space worth the effort. After space-reclamation, the collection cycle restarts with another young-only phase. As backup, if the application runs out of memory while gathering liveness information, G1 performs an in-place stop-the-world full heap compaction (Full GC) like other collectors. Garbage Collection Pauses and Collection Set
G1 performs garbage collections and space reclamation in stop-the-world pauses. Live objects are typically copied from source regions to one or more destination regions in the heap, and existing references to these moved objects are adjusted. For non-humongous regions, the destination region for an object is determined from the source region of that object: Objects of the young generation (eden and survivor regions) are copied into survivor or old regions, depending on their age. Objects from old regions are copied to other old regions. Objects in humongous regions are treated differently. G1 only determines their liveness, and if they are not live, reclaims the space they occupy. G1 only moves humongous objects in a very slow last-resort collection effort. Remembered Set
To evacuate the collection set G1 manages a remembered set: the set of locations outside the collection set that contain references into the collection set. When an object from the collection set moves during garbage collection, any other references to that object from outside the collection set need to be changed to point to the new location of the object. The remembered set entries represent approximate locations to save memory: often references close together reference objects close together. G1 logically partitions the heap into cards, by default 512 byte sized areas. Remembered set entries are compressed indexes of these cards. G1 initially manages this remembered set on a per-region basis: every region contains a per-region remembered set, the set of locations with potential references into this region. During garbage collection, the remembered set for the entire collection set is generated from these. The remembered sets are created mostly lazily: between the Remark and Cleanup pause G1 rebuilds the remembered set of all marking collection set candidate regions. Other than that G1 always maintains remembered sets for young generation regions as they are collected at every collection. Collection Set
The collection set is the set of source regions to reclaim space during garbage collection. Independent of the garbage collection type, the collection set consists of different kinds of regions: Young generation regions, Humongous regions. See about the restrictions, Collection set candidate regions. These are old generation regions that G1 determined to be good candidate regions for garbage collection due to their high collection efficiency. This efficiency is calculated from the amount of free space, where regions with little live data are preferred over regions that contain mostly live data, and the connectivity to other regions, low connectivity being preferred over high connectivity. There are two sources for old generation collection set candidate regions: from whole heap analysis, i.e., the marking, when G1 has good information about the liveness and connectivity of all old generation regions, and regions that experienced evacuation failure. The former region's efficiencies have been determined directly using up-to-date liveness and connectivity data gathered in the preceding concurrent mark. The objects that G1 has not been able to evacuate during an evacuation failure are typically very few. This makes them very efficient regions to collect by default. Garbage Collection Process
A garbage collection consists of four phases.
The Pre Evacuate Collection Set phase performs some preparatory work for garbage collection: disconnecting TLABs from mutator threads, selecting the collection set for this collection as described in , and other small preparatory work.
During Merge Heap Roots G1 creates a single unified remembered set for later easier parallel processing from the collection set regions. This removes many duplicates from the individual remembered sets that would otherwise be needed to be filtered out later in a more expensive way.
The Evacuate Collection Set phase contains the bulk of the work: G1 starts moving objects starting from the roots. A root reference is a reference from outside the collection set, either from some VM internal data structure (external roots), code (code roots) or from the remainder of the Java heap (heap roots). For all roots, G1 copies the referenced object in the collection set to its destination, processes its references into the collection set as new roots until there are no more roots. Individual timing for these phases can be observed with -Xlog:gc+phases=debug logging in the Ext Root Scanning, Code Root Scan, Scan Heap Roots, and Object Copy sub-phases respectively. G1 may optionally repeat main evacuation phases for optional collection sets.
The Post Evacuate Collection Set consists of clean-up work including reference processing and setup for the following mutator phase. These phases correspond to the phases printed with -Xlog:gc+phases=info logging. (责任编辑:) |


