[JVM] 5. Gangster small Bai Xuecheng garbage collector and memory allocation strategy

Introduced in front of the garbage collection algorithm, then we introduce strategies garbage collection and memory allocation. Is there a silver bullet Niubi collector as fit as all the scenes? Obviously, there can be no, or I did not need to engage in a separate article to introduce a garbage collector. Familiar with the advantages and disadvantages of different collectors, flexible use in the actual scene, is king.

Before starting introduce garbage collector, we can spoilers points:

  • Depending on the characteristics of generational collector may be different. Some collectors can be used simultaneously old and new generation's, but there are times when you need to, respectively, the appropriate choice for the new generation of collectors or old's. In general, the new generation of high frequency collecting collector should be used in the efficient performance of the collector; s relatively low collector to collect old times, more sensitive to the space, the collector should be avoided selected based replication algorithm.
  • At the time garbage collection execution, the application needs to run pause .
  • It can be collected serially, or in parallel to collect.
  • If you can do concurrent collector (application need not pause), it is definitely a very wonderful thing.
  • If the collection of behavioral control, that is a very wonderful thing.

I hope you read with the following questions, targeted reading, might gain more.

  1. Why is there a silver bullet regressed collector as fit as all the scenes?
  2. Contrast CMS and G1, you know he difference between the two of you?
  3. Why CMS can only be used as a collector's old, but can not be applied in the collection of the new generation?
  4. Why JVM generational age 15? Instead of 16, 20 like it?
  5. "Dynamic target age determination" has a " sinkhole " Oh, what a pit it?

1 garbage collector

GC thread and application threads to remain relatively independent, when the system needs to perform garbage collection tasks, stop the worker thread, and then command GC thread work. To collect the serial mode of operation, referred to as a serial collector (i.e. Collector Serial) . Opposed parallel mode of operation is to trap, called a parallel collector (i.e. Collector Paraller) .

1.1 serial collector: Serial

Serial collector single-threaded approach to gather and work in the GC thread, the thread system does not allow applications to bother. In this case, the application enters a suspended state , namely Stop-the-world.

Stop-the-world pause length of time, it is a measure of an important indicator of the level of collector performance of.

It is for the new generation of the garbage collector, based on the label - replication algorithm .

1.2 parallel collector: ParNew

Parallel collector takes advantage of multiple processors, the use of multiple threads in parallel GC collection. One can imagine a number of GC threads of execution is clearly more efficient than using only one GC thread of execution. In general, as compared with the serial accumulator, operating in a multiprocessor environment under the parallel collector can be greatly shortened Stop-the-world time.

For the new generation of the garbage collector, Mark - replication algorithm , can be seen as a multi-threaded version of Serial

1.3 throughput priority collectors: Parallel Scavenge

For the new generation of the garbage collector, Mark - replication algorithm , and ParNew similar, but more emphasis on throughput. On the basis of ParNew evolved Parallel Scanvenge collector known as the "throughput priority" collector. Throughput is the total elapsed time and the CPU CPU time used to run user code ratio, i.e., a certain time = running user code / (user code running time + time garbage collection) . Such as virtual machine running total of 100 minutes, the garbage collector to spend one minute that the throughput is 99%.

Parallel Scanvenge ParNew collector provided on the basis of a set of parameters used to configure a desired collection time or throughput, and this goal collected.

Approximate range may be controlled by a certain VM options:

  • -XX: MaxGCPauseMills: collect the desired time limit. Used to control the impact on the application to collect pause.
  • -XX: GCTimeRatio: GC ratio of a desired total time, used to control the throughput.
  • -XX: UseAdaptiveSizePolicy: automatic generation of size adjustment policies.

But pay attention to pause time and throughput These two objectives are contrary to reduce the pause time will also cause a decrease in throughput. It is necessary to target control in a reasonable range.

1.4 Serial Old collectors

Serial Serial Old is a collector of old's version, single-threaded collectors, using the mark - Collation Algorithm . The main significance of this is that the collector to the virtual machine in the Client mode to use.

1.5 Parallel Old collectors

Parallel Old is Parallel Scanvenge collector old's version, multithreaded collector, using a mark - sort algorithm .

1.6 CMS collector

CMS (Concurrent Mark Sweep) is a collector for the shortest recovery time objectives pause collector .

CMS collector acts only on old's collection is based on mark - sweep algorithm , and its operation process is divided into four steps:

  • The initial mark (CMS initial mark)
  • Concurrent mark (CMS concurrent mark)
  • Relabeled (CMS remark)
  • Concurrent Clear (CMS concurrent sweep)

Among them, the initial marker, re-mark these two steps still need to Stop-the-world. The initial mark just mark what objects can be linked directly to the GC Roots, very fast, concurrent mark phase is in progress GC Roots Tracing, and re-mark phase is due for a correction period concurrent mark the user program continues to operate and generate leads mark mark them that part of the object changes, dwell time at this stage generally a little longer than some of the initial stage, but shorter than the concurrent mark time.

Split pipelined fashion CMS collection cycle, the time-consuming application with the holding operation unit concurrently executing threads. Only those necessary for STW operation unit to perform alone carry out, control units running at the right time, and to ensure that only a short time to complete. In this way, the entire collection period, only two brief pause (initial marking and re-marking) , to achieve the purpose approximate concurrent .

CMS collector advantages : concurrent collection, low standstill.

CMS collector disadvantages :

  • CMS collector is very sensitive to CPU resources.
  • CMS garbage collector can not handle floating (Floating Garbage).
  • CMS collector is based on the mark - sweep algorithm, the algorithm has a shortcoming.

CMS collector has been able to achieve concurrency, simply because based "mark - sweep" algorithm and the decomposition process of the algorithm is fine-grained . Chapter introduced earlier mark - sweep algorithm will generate a lot of memory fragmentation, this new generation is unacceptable, and therefore does not provide a new generation of collectors CMS version.

1.7 G1 collector

G1 redefines the heap, generational break the original model, the heap is divided into one area. The goal is not necessary within the scope of the collection during the full stack, which is its most notable features. The benefits of zoning is to bring the model collection pause times predictable: the user can specify the collection is finished how long. Namely G1 provides near real-time collection of properties.

Wherein G1 and CMS comparison are as follows:

feature CMS G1
Concurrent and generational Yes Yes
To maximize the release of heap memory Yes no
Low latency Yes Yes
Throughput high low
Compaction Yes no
Predictability Strong weak
Physical isolation and the new generation of old age no Yes

G1 has the following characteristics:

  • Parallel and Concurrent : G1 can take full advantage of multi-CPU, multi-core hardware advantages in the environment, the use of multiple CPU to shorten the Stop-the-world pause time, some of the other collectors need to pause the original Java thread execution GC operation, G1 collector can still concurrent let Java programs continue to run the way.
  • Generational collection
  • Spatial integration: the mark and CMS - Clear different algorithms, G1 is based on the whole mark - Collation Algorithm collector implementation, is based on the "from the local (between two Region) view copy " algorithm implementation. In any case, these two algorithms mean no memory space debris during operation of the G1, the collection offers regular memory available. This feature is conducive to long-running programs, not because they can not find a contiguous memory space prematurely trigger the next GC when allocating large objects .
  • Predictable pause: This is an advantage with respect to G1 CMS, reducing the dwell time is a common G1 and CMS concerns.

The scope of the collection of other collectors before the G1 is a whole new generation or old's, and G1 is no longer the case. In the structural design of the stack when, Gl breaking unit area in a conventional fixed mode range of the new generation collected or old age, Gl heap into a plurality of equal size, each unit is called Region. Region is an address of the contiguous memory space, G1 modules composition shown below:

G1 stack layout .png Region

G1 collector entire Java heap is divided into separate areas (Region) a plurality of equal size, although there are also retained the concept of the new generation and old age, but the new generation and the old year is no longer physically separated, and they are part of Region (not necessarily consecutive) set. G1 collector model has been able to establish a predictable pause time, because it can be planned to avoid heap garbage collection across the region throughout the Java . G1 will pass a reasonable computational model to calculate the cost of each Region collected and quantized, so that, at a given collector case where the "pause" time limit, always select a proper set as the collection Regions goal, let collection overhead meet this constraint, in order to achieve the purpose collected in real time.

For intends migrated from CMS or ParallelOld collector application, in accordance with the official recommendations, if found to comply with the following characteristics can be considered to replace the G1 collector in pursuit of better performance:

  • Real-time data takes up more than half of the heap space;
  • Object allocation ratio or "promoted" speed change significantly;
  • Desire to eliminate lengthy or GC pauses (more than 0.5 - 1 sec).

原文如下:
Applications running today with either the CMS or the ParallelOld garbage collector would benefit switching to G1 if the application has one or more of the following traits.

  • More than 50% of the Java heap is occupied with live data.
  • The rate of object allocation rate or promotion varies significantly.
  • Undesired long garbage collection or compaction pauses (longer than 0.5 to 1 second)

G1 collection operation process is as follows:

  • The initial mark (Initial Marking) : When an object is just what marks GC Roots can be directly linked to, and modify the value of TAMS (Next Top at Mark Start), so that the next phase of user programs run concurrently, you can correct available in the Region create a new object, this stage requires a standstill thread, but it takes very short .
  • Concurrent mark (Concurrent Marking) : GC Roots from the beginning of the heap objects reachability analysis to identify live objects, this stage takes a long time , but may be complicated by the user program execution.
  • The final mark (Final Marking) : amendments to the user program during concurrent mark due to the continued operation which led to marked changes in that part of the generated record mark, the virtual machine during this time the object changes recorded in the thread Remembered Set Logs inside, marking the final stage of the need to Remembered Set Logs data incorporated into Remembered Set in this stage need to stop thread, but can be executed in parallel .
  • Filter Recycling (Live Counting the Data and Evacuation) : First, the recovery value and cost of each Region to sort, to develop recycling programs according to user desired GC pauses. This stage can be done concurrently with the user program, but because only recover a portion of Region, time is user-controllable, but stop user threads will greatly enhance the collection efficiency.

We can look at the official documentation outlook for the G1 (this description is relatively simple English, I do not translated):

Future:
G1 is planned as the long term replacement for the Concurrent Mark-Sweep Collector (CMS). Comparing G1 with CMS, there are differences that make G1 a better solution. One difference is that G1 is a compacting collector. G1 compacts sufficiently to completely avoid the use of fine-grained free lists for allocation, and instead relies on regions. This considerably simplifies parts of the collector, and mostly eliminates potential fragmentation issues. Also, G1 offers more predictable garbage collection pauses than the CMS collector, and allows users to specify desired pause targets.

2 memory allocation strategy

Memory allocation object, speaking to the general direction, that is, heap allocation on (but possibly through JIT compiler break after a scalar type and, indirectly, on the stack allocation ), the main targets allocated on the Eden area of the new generation , if you start the thread-local buffer allocation, priority will be assigned threads on TLAB. In rare cases it may be allocated directly in the old era.

2.1 Object priority allocation in Eden

In most cases, the new generation of object allocation in Eden area. When the Eden area is not enough space allocated, the virtual machine will launch a Minor GC (the previous chapters have introduced Minor GC). But there is also a case in memory guarantee mechanism , the object can not be placed directly into the old era.

2.2 large objects directly into the old year

Refers to the large object Java object requires a large amount of contiguous memory space, the most typical of large objects is the kind of long strings and arrays.

Virtual machines provide a -XX: PretenureSizeThreshold parameters, so that the object is larger than the set value allocated directly in the old years . The purpose is to avoid a lot of memory replication occurs in the area between Eden and two Survivor areas.

2.3 Long-term survival of the object will enter the old year

Virtual machine for each object defines a target age (Age) counter. If the object was born in Eden, and after the first Minor GC still alive, and can accommodate Survivor, it will be moved to Survivor space and age is set to 1. Survivor objects in the area did not go through a Minor GC, plus 1 year of age on, when reached the age of 15 years old (the default value), it will be promoted to the old era.

Object promoted's old age threshold, parameters can be -XX: MaxTenuringThreshold settings.

Next we have to answer why why JVM generational age 15? Instead of 16, 20 like it?

Why can not the other is not really the number (except 15), indeed Chenqie do ah!

The way it is, the object HotSpot VM runtime header part for storing data of the object itself, such as a hash code (HashCode), GC generational age lock state flag thread holds the lock, the thread ID bias, deflecting timestamp, this part of the length of the data in 32-bit and 64-bit virtual machine (not open compressed pointer), respectively, and for the 32bit 64bit, officially called " Mark Word ."

For example, in a 32-bit HotSpot virtual machine, if the object is in the unlocked state, then the Mark Word 32bit 25bit space for storing objects hash code, 4bit generational Age for storing objects , for storing 2bit lock flag, 1bit fixed at 0.

You understand why yet? Generational accounting for four of the age of the subject, that is, 0000, a maximum of 1111 is up to 15, 16, 20 and impossible to like the.

2.4 Age Determination of dynamic objects

In order to better adapt to different conditions program memory, the virtual machine does not always require cash over the age must be reached before promotion MaxTenuringThreshold years old.

Meets one of the following conditions, the object can be promoted to the old year:

  • 1. Object reached the age MaxTenuringThreshold (default 15) years old can be promoted.
  • 2. If the sum of the same age Survivor space is larger than half the size of all the objects in space Survivor, age greater than or equal to the object that age you can go directly years old, no need to wait until the age MaxTenuringThreshold in requirements.

Many articles have just taken note of the situation described above (including Ali middleware public numbers issued an article just this simple introduction, then give them back to stay off the words to explain the situation), but only if so understanding, you will find contrary to this provision in the actual memory recovery.

For small chestnuts, such as target age 5 accounted for 28%, accounting for 20% of the age of 6, ages 7, 52%, according to two criteria, objects can not enter old age, but up 100% Survivor have ah ?

We can focus on this parameter TargetSurvivorRatio, the survival rate target, the default is 50% . That means, roughly, from small to large accumulation of age, such as adding a certain age after (such as chestnuts in the age of 7), total occupancy of more than ** Survivor * TargetSurvivorRatio space of time, from the beginning of this age and the age of an object larger than necessary enters years old (ie chestnuts in the age of 7 objects). Age estimation of dynamic objects, mainly by controlling the TargetSurvivorRatio this parameter. Also considered were age from small to large and cumulative, rather than an object the size of age. **

2.5 space allocation guarantees

Minor GC before it occurs, first check whether the old virtual machine's maximum available contiguous space is greater than all the objects in the new generation of total space, if this condition is met, then the Minor GC to ensure that it is safe. If not satisfied, the virtual opportunity to review whether to allow HandlePromotionFailure settings guarantee failure. If allowed, it will continue to check whether old's maximum available contiguous space larger than previous years old was promoted to the average size of the object, if greater than, will attempt to conduct a Minor GC, despite the Minor GC is the risk of; if less than, or HandlePromotionFailure settings do not allow risk, then it should be changed once a Full GC .

What is said above, the risk is it? We know that the use of the new generation of copying collection algorithm, but for memory utilization, use only one Survivor space to rotate as a backup, so when the situation a lot of objects in the Minor GC still alive (the most extreme case is the memory recovery appears newborn on behalf of all the objects are alive), you need to be allocated guarantee years old, can not accommodate the Survivor objects directly into the old era.

3. Summary Brain Mapping

Memory allocation strategy .png

Brain Mapping too. For high-definition full-sized image, please leave a message inform.

Guess you like

Origin www.cnblogs.com/heyonggang/p/11427994.html