G1-收藏
摘录自:http://www.drdobbs.com/jvm/g1-javas-garbage-first-garbage-collector/219401061?pgno=1
?
Parallelism and Concurrency?
?
When speaking about garbage collection algorithms, parallelism describes the collector's ability to perform its work across multiple threads of execution. Concurrency describes its ability to do work while application threads are still running. Hence, a collector can be parallel but not concurrent, concurrent but not parallel, or both parallel and concurrent.
?
The Java parallel collector (the default) is parallel but not concurrent as it pauses application threads to do its work. The CMS collector is parallel and partially concurrent as it pauses application threads at many points (but not all) to do its work. The G1 collector is fully parallel and mostly concurrent, meaning that it does pause applications threads momentarily, but only during certain phases of collection.
?
?
Garbage-First is a server-style garbage collector, targeted for multi-processors with large memories, that meets a soft real-time goal with high probability [Detlefs04]. It does this while also achieving high throughput, which is an important point when comparing it to other real-time collectors.
?
The G1 collector divides its work into multiple phases, each described below, which operate on a heap broken down into equally sized regions (see Figure 1). In the strictest sense, the heap doesn't contain generational areas, although a subset of the regions can be treated as such. This provides flexibility in how garbage collection is performed, which is adjusted on-the-fly according to the amount of processor time available to the collector.
?
Figure 1: With garbage-first, the heap is broken into equally sized regions.?
Regions are further broken down into 512 byte sections called cards (see Figure 2). Each card has a corresponding one-byte entry in a global card table, which is used to track which cards are modified by mutator threads. Subsets of these cards are tracked, and referred to as Remembered Sets (RS), which is discussed shortly.
?
Figure 2: Each region has a remembered set of occupied cards.?
The G1 collector works in stages. The main stages consist of remembered set (RS) maintenance, concurrent marking, and evacuation pauses. Let's examine these stages now.
?
?
Each region maintains an associated subset of cards that have recently been written to, called the Remembered Set (RS). Cards are placed in a region's RS via a write barrier, which is an efficient block of code that all mutator threads must execute when modifying an object reference. To be precise, for a particular region (i.e., region a), only cards that contain pointers from other regions to an object in region a are recorded in region a's RS (see Figure 3). A region's internal references, as well as null references, are ignored.
?
Figure 3: A region's RS tracks live references from outside the region.?
In reality, each region's remembered set is implemented as a group of collections, with the dirty cards distributed amongst them according to the number of references contained within. Three levels of courseness are maintained: sparse, fine, and course. It's broken up this way so that parallel GC threads can operate on one RS without contention, and can target the regions that will yield the most garbage. However, it's best to think of the RS as one logical set of dirty cards, as the diagrams show.
?
?
Concurrent marking identifies live data objects per region, and maintains the pointer to the next free byte, called top. There are, however, small stop-the-world pauses (described further below) that occur to ensure the correct heap state. A marking bitmap is maintained to create a summary view of the live objects within the heap. Each bit in the bitmap corresponds to one word within the heap (an area large enough to contain an object pointer; see Figure 4). A bit in the bitmap is set when the object it represents is determined to be a live object. In reality there are two bitmaps: one for the current collection, and a second for the previously completed collection. This is one way that changes to the heap are tracked over time.
?
Figure 4: Live objects are indicated with a marking bitmap.?
Marking is done in three stages:
?
?
?
?
?
?
All of this work is done so that objects that are in the collection set are reclaimed as part of the evacuation process. Let's examine this process now.
?
?
This step is what it's all about -- reclaiming dead objects and shaping the heap for efficient object allocation. The collection set of regions (from the Concurrent Marking process defined above) forms a subset of the heap that is used during this process. When evacuation begins, all mutator threads are paused, and live objects are moved from their respective regions and compacted (moved together) into other regions. Although other garbage collectors might perform compaction concurrently with mutator threads, it's far more efficient to pause them. Since this operation is only performed on a portion of the heap -- it compacts only the collection set of regions -- it's a relatively quick, low-pause, operation. Once this phase completes, the GC cycle is complete.
?
To help limit the total pause time, much of the evacuation is done in parallel with multiple GC threads. The strategy for parallelization involves the following techniques:
?
?
Note: The CMS and parallel collectors, described earlier, also use work competition and work stealing techniques to achieve greater efficiency.
?
?
The G1 collector is still considered experimental, but can be enabled in Java SE 6 Update 14 with the following two command-line parameters:
?
?12-XX:+UnlockExperimentalVMOptions
-XX:+UseG1GC
?
Much of the G1 processing and behavior can be controlled by explicitly setting optional command-line parameters. See the sidebar entitled "Tuning the G1 Collector" to tune G1 behavior.
?
Tuning the G1 Collector
?
?
?
Let's review some command-line parameters that enable you to tune the behavior of G1. For instance, to suggest a GC pause time goal, use the following parameter:
?
?1-XX:MaxGCPauseMillis=50 (for a target of 50 milliseconds)
?
With G1, a time interval can be specified during which a GC pause applies. In other words, no more than 50 milliseconds out of every second:
?
?1-XX:GCPauseIntervalMillis=1000 (for a target of 1000 milliseconds)
?
Of course, these are only targets and there are no guarantees they will be met in all situations. However, G1 will attempt to meet these targets where it can.
?
Alternatively, the size of the young generation can be specified explicitly to alter evacuation pause times:
?
?1-XX:+G1YoungGenSize=512m (for a 512 megabyte young generation)
?
To run G1 at its full potential, add the following two parameters:
?
?12-XX:+G1ParallelRSetUpdatingEnabled
-XX:+G1ParallelRSetScanningEnabled
?
However, Sun warns that as of this version of G1, the use of these parameters may produce in a race condition and result in an error. However, it's worth a try to see if your application works safely with them set. If so, you'll benefit from the best GC performance that G1 can offer.
?
?
—EJB
?
In terms of GC pause times, Sun states that G1 is sometimes better and sometimes worse than CMS. As G1 is still under development, the goal is to make G1 perform better than CMS and eventually replace it in a future version of Java SE (the current target is Java SE 7). While the G1 collector is successful at limiting total pause time, it's still only a soft real-time collector. In other words, it cannot guarantee that it will not impact the application threads' ability to meet its deadlines, all of the time. However, it can operate within a well-defined set of bounds that make it ideal for soft real-time systems that need to maintain high-throughput performance.
?
If your application requires guaranteed real-time behavior even with garbage collection, your only choice is a real-time garbage collector such as those that come with Sun's Java RTS or IBM's WebSphere RT products. However, if low pause times and soft real-time behavior is your goal, the G1 collector should suit it well.
?
?
[Bruno09] Bruno, Eric, and Bollella, Greg, Real-Time Java Development with Java RTS, Pearson Publishing, 2009
?
[Detlefs04] Detlefs, et. al.,?Garbage-First Garbage Collection, Sun Microsystems Research Laboratories, 2004.
?
[McCarthy58] McCarthy, John,?LISP: A Programming System for Symbolic Manipulations, Communications of the ACM, 1958.