JVM notes "Four" seven common garbage collectors

Garbage collector

1. Serial (new generation)

1. Single thread (all other threads need to be suspended during collection)

2. The default collector in client mode is also a good choice

advantage:

1. Simple and efficient (compared to the single thread of other collectors)

2. When memory resources are limited, additional memory consumption is minimal

3. Because there is no overhead of thread interaction, the highest single-thread efficiency can be obtained

Insert picture description here

2. ParNew (new generation)

It is a multi-threaded version of Serial.

Multi-threaded parallel collection, the rest is the same as Serial

  1. Multi-threaded parallel collection (user process needs to wait)

  2. Currently only one who can work with CMS collector work

    Use -XX:+UseConcMarkSweepGCparameters, use ParNew + CMS + Serial Old collector combination, at this time ParNew is the default collector of the new generation

  3. The number of collection threads enabled by default is the same as the number of processor cores.

    You can use -XX:ParallelGCThreadsparameters to limit the number of threads

The efficient use of system resources during garbage collection is very beneficial.

Insert picture description here

Combinations cancelled after JDK9 :

  1. ParNew + CMS (not canceled, but not recommended as a collector combination in server mode)
  2. ParNew+Serial Old
  3. Serial+CMS

So only ParNew and CMS can be combined, it can also be understood that ParNew is incorporated into CMS

3. Parallel Scanvenge (new generation)

1. Mark-based replication

2. Parallel collection

3. Focus on throughput use Household generation code Time between / ( + ) User code time / (user code time + garbage collection time)

Suitable for tasks in the background without too much interaction

Related parameters

  1. -XX:MaxGCPauseMillis

    Number of milliseconds greater than 0

    Maximum garbage collection pause time (at the expense of young generation size, each pause time is short, but causes GC to be more frequent and reduces throughput)

  2. -XX:GCTimeRatio

    0-100 Integer: The ratio of garbage collection time to total time. The original book concept is a bit messy and not easy to understand.

    But as long as the parameter value N is calculated by the formula: 1/1 + N , the ratio of garbage collection time to the total time is calculated, and N itself is not the ratio

    If the parameter is 49: Throughput = 1-1 / (1 + 49) = 1-1 / 50 = 98% 1/50 is the proportion of garbage collection time

  3. -XX:UseAdaptiveSizePolicy

    Turn on this parameter, you do not need to specify the new generation size (-Xmn), -XX: SurvivorRatio, -XX: PretenureSizeThreshold and other parameters, you only need to specify an optimization goal: such as the maximum heap -Xmx, and the above parameters 1 or 2, specify the focus The pause time or throughput, the system will dynamically adjust the relevant parameters, this is called the adaptive adjustment strategy of garbage collection

4. Parallel Old

Old generation version of Parallel Scavenge

  1. Multi-threaded concurrent collection
  2. Mark-based sorting
  3. Focus on throughput (Parallel Scanvenge + Parallel Old)

Insert picture description here

5. Serial Old

Old generation version of Serial

1. Single thread

2. Marking up

3. Same as Serial for client mode

4. As a backup plan when the CMS collector fails

Working process diagram reference one, Serial collector

6. CMS (Old Age)

Pay attention to the pause time —> Give users a good interactive experience in server mode such as B / S

Operation process

1. Initial Mark

2. Concurrent Mark

3. Remark

4. Concurrent sweep (Concurrent Sweep)

1. Initial marking: marking objects directly associated with GCRoots, fast

2. Concurrent marking: start traversing the entire object graph from the objects directly associated with GCRoots, without suspending user threads, and running concurrently with GC threads, which takes a long time

3. Re-marking: Fix the object whose mark changes due to user thread during concurrent marking

4. Concurrent removal: delete the objects marked as dead, and can run concurrently with the user program

Insert picture description here

Disadvantages

  1. Sensitive to CPU resources

    The number of recycling threads started by the CMS by default is ** (cpu number +3) / 4 **. Therefore, the smaller the number of CPUs, the lower the execution speed of user programs.

  2. Unable to handle floating garbage

    Because the user program is still to be executed in the two concurrent stages, new garbage objects will be generated, and these objects can only be collected at the next GC because they appear after the mark. These are floating garbage.

    • The CMS cannot wait until the old generation objects are full.

    • Parameters -XX:CMSInitiatingOccupancyFraction: Set the threshold triggered by CMS. In jdk1.6, the default threshold of CMS is raised to 92%.

    • -XX: + UseCMSInitiatingOccupancyOnly : The default is false. If it is not used, the threshold set above will be used only once, and it will be automatically adjusted later; if used, the set threshold will always be used.

    You can use these two parameters together:

    -XX:CMSInitiatingOccupancyFraction=70 -XX:UseCMSInitiatingOccupancyOnly=true

    • When the memory reserved during the operation of the CMS cannot meet the needs of the program, "Concurrent Mode Failure" will appear, and then the Serial Old collector will be temporarily enabled for old generation garbage collection, so the pause time is very long, so the parameter value is set too High is easy to cause a lot of "Concurrent Mode Failure".

    have to be aware of is:

    intx CMSInitiatingOccupancyFraction = -1 {product}

    The default value of this parameter is -1 in jdk1.8, and 92% is calculated as follows:

        void ConcurrentMarkSweepGeneration::init_initiating_occupancy(intx io, uintx tr) {
          assert(io <= 100 && tr <= 100, "Check the arguments");
          if (io >= 0) {
            _initiating_occupancy = (double)io / 100.0;
          } else {
            _initiating_occupancy = ((100 - MinHeapFreeRatio) +
                                     (double)(tr * MinHeapFreeRatio) / 100.0)
                                    / 100.0;
          }
        }
    
    
    1. If the parameter value is greater than or equal to 0, the percentage is used as the parameter

    2. If less than 0, in accordance with MinHeapFreeRatioand trcalculated according to the above formula, MinHeapFreeRatiowill reduce the (invalid -Xms = -Xmx) heap to the heap size to achieve this percentage, i.e. TR CMSTriggerRatio, the default value is found to view these two parameters:

      uintx MinHeapFreeRatio             = 40                           {manageable}
      uintx CMSTriggerRatio               = 80                           {product}
      

      Therefore, the percentage of triggered CMS is: ((100-40) + (80 * 40) / 100) /100=0.92=92%

  3. Space debris exists because it is based on a mark-and-sweep algorithm

    • -XX: + UseCMSCompactAtFullCollection : defragmentation at FullGC, exclusive cannot be concurrent, will make the pause time longer, JDK9 began to be abandoned
    • -XX: + CMSFullGCsBeforeCompaction set to defragment after a few FullGC

to sum up

The CMS trigger threshold cannot be too large, otherwise it will cause concurrency failure, and it may not be too small, which will cause frequent CMS triggers, and the initial marking and re-marking (time-consuming) of CMS will cause Stop The World. 70

七、 Garbage First(G1)

1. Facing server (JDK9 start as the default garbage collector in server mode, CMS were Deprecate)

2. For any part of the heap memory to form a collection (CSet), the standard of measurement becomes which piece of memory has the largest amount of garbage stored, and the maximum recovery revenue

  • G1 divides the heap into independent regions of equal size (Region). Each region can play eden, survivor, and the old generation. The collector can take different strategic regions for different role regions. , That is, each time the memory space recovered is an integer multiple of the region

  • The Humongous (regarded as the old generation) region in the Region is used to store large objects , as long as an object with more than half the capacity of a region can be called a large object

    Parameter -XX:G1HeapRegionSizeparameters can specify the size of each region, range: 1M~32Mpower of 2 times

    Oversized objects over the entire region are placed in consecutive Homongous

  • G1 maintains a priority list according to the value of garbage in each region (that is, the experience value of the space obtained and the time required for recycling), and preferentially recycles the regions with large value according to the pause time set by the user

some questions

  1. G1 uses memory sets to avoid whole stack scans

  2. The concurrent marking phase ensures that the collection thread and the user thread do not interfere with each other:

    G1 uses the original snapshot method to ensure that when the user thread changes the reference relationship, the original object graph structure is not destroyed, ensuring the correctness of the mark. Refer to 4.6 above (original snapshot)

    • G1 designed two TAMS (Top At Mark Start) pointers for each region
    • Partition space for allocation of new objects generated during concurrent collection
    • New object address must be above two pointer positions
    • G1 objects above this address are alive by default
    • When the speed of allocating objects exceeds the recovery speed, the user thread is frozen and a Full GC is generated

working process

  1. Initial mark

    Mark the objects directly related to GCRoots, modify the TAMS pointer value, so that when the user process runs concurrently in the next stage, new objects can be allocated in the available regions. As before, STW is required, but it takes a short time and is completed synchronously with the help of MinorGC, so G1 does not require additional pauses at this stage.

  2. Concurrent mark

    From the objects directly related to GCRoots, the accessibility analysis of the object graph is performed to find out the objects to be recovered, which takes a long time.

    It can be executed concurrently with the user process. After the scan is completed, it is necessary to reprocess the concurrent referenced changed objects recorded by SATB (original snapshot).

  3. Final mark

    Another short pause on the user thread is used to process a small number of SATB records left after the concurrency phase ends.

  4. Screening and recycling

    • Update region statistics
    • Sort all regions by recycling value and cost
    • Make a recycling plan according to the pause time set by the user
    • Freely select multiple regions as back collection, copy the surviving objects of the determined region to another empty region, and then clean the entire old region
    • The user process needs to be suspended , and multiple collectors can be executed in parallel.

    Update-> Sort-> Develop Recycling Plan-> Select Back Collection-> Copy Cleanup (Parallel, STW)

    In addition to concurrent marking, all user processes need to be stopped.

The schematic diagram is as follows:
Insert picture description here

Features and advantages:

  1. Get the highest possible throughput with a controlled delay.

  2. The user can specify the expected (to meet the actual) pause time

    The default is 200ms, which cannot be too low. Too low will cause each selected collection to occupy only a small part of the heap, and the recovery speed cannot keep up with the allocation speed, resulting in Full GC

  3. Partition (region) management heap memory

  4. Determine return to collection according to recycling efficiency

  5. The whole is based on mark-organization, and the partial is based on mark-copy, without fragmentation

Disadvantages (compared to CMS)

  1. Memory usage

    Because each region needs to maintain a card table to solve the cross-generation reference problem, the memory set may account for more than 20% of the entire heap. CMS has only one card form.

  2. High additional execution load

    As with CMS, write barriers are used to maintain the card table.

    In addition to using the post-write barrier like CMS (using synchronization directly), G1 also requires the pre-write barrier to implement the original snapshot algorithm, which creates additional burden. (Use message queue)

Small memory application CMS has a high probability due to G1

======================================================================

Other related notes:

JVM notes (1) java memory area and memory overflow and object creation, layout and positioning

JVM notes (2) The life and death of objects and the four major references to java

JVM notes (3) garbage collection algorithm and HotSpot algorithm implementation (security point, memory set and card table, write barrier, three-color mark, etc.)

JVM notes (five) class loading mechanism, class loader and parent delegation mechanism

================================================================

Reference:
"In-depth understanding of the third edition of java virtual machine"

Published 75 original articles · won praise 13 · views 8369

Guess you like

Origin blog.csdn.net/weixin_43696529/article/details/104884777