JVM-GC garbage collector

Object reference judgment

https://blog.csdn.net/sunjin9418/article/details/79603651

1> Reference counting algorithm

Add a reference counter to the object. Whenever there is a reference to it, the counter is incremented by 1, and when the reference is invalid, the counter is decremented by 1. An object whose counter is 0 at any time cannot be used.

## The problem that the reference counting algorithm cannot make circular references between objects

2> GC Roots reachability analysis algorithm

Using the "GC Roots" object as the starting point, searching downwards from these nodes, the path that the search traverses is called the Reference Chain. When an object is connected to the GC Roots without any reference chain, it proves that the object is not Used

In Java, the following types of objects can be used as GC Roots:

  1. Objects referenced in the virtual machine stack (local variable table in the stack frame);
  2. Objects referenced by static properties of the class in the method area;
  3. Objects referenced by constants in the method area;
  4. Objects referenced by JNI (native method) in the native method stack;

Garbage collection algorithm

Mark-clear algorithm

  • Marking: The process of judging garbage objects is the marking process
  • Cleanup: clean up objects marked as garbage

The algorithm has two shortcomings:

  1. Inefficient marking and removal
  2. Space problem, after the mark is cleared, a large number of discontinuous memory fragments will be generated. Too much space fragmentation may cause the problem of failure when allocating large blocks of memory in the future, which will trigger another garbage collection operation

Copy algorithm

Divide the available memory into two equal parts according to the capacity, and only use one of them at a time. When one piece of memory is used up, copy the surviving objects to another piece, and then clear the used memory space at once

The default ratio of Eden to Survivor1 and Survivor2 of HotSpot virtual machine is 8:1:1

Mark-up algorithm

Mark-Compact (Mark-Compact) algorithm, the marking process is the same as the mark-clear algorithm, but the subsequent steps are not to directly clean up recyclable objects, but to sort out the surviving objects, move the surviving objects to one end, and then directly clean up Memory outside the boundary

Generational collection algorithm

Current virtual machines all use the "generational collection" algorithm, which just divides the memory according to the life cycle of the object.
Generally, the Java heap space is divided into the new generation and the old generation, so that the most suitable collection algorithm can be adopted according to the characteristics of each generation.

In the new generation, a large number of objects will die every garbage collection, and only a few will survive. In this way, the replication algorithm can be selected, and garbage collection can be completed by copying a small number of surviving objects. In the old age, the survival rate of the object is high, and there is no extra space to guarantee it, it must be reclaimed by the mark-clear or mark-sort algorithm

HotSpot Generational Collector-7 types

https://www.cnblogs.com/swordfall/p/10734403.html
New Generation Collector: Serial, ParNew, Parallel Scavenge
Old Generation Collector : Serial Old, Parallel Old, CMS
Whole Collector: G1

If there is a connection between the two collectors, it means that they can be used together.
Insert picture description here
Available combinations: Serial/Serial Old, Serial/CMS, ParNew/Serial Old, ParNew/CMS, Parallel Scavenge/Serial Old, Parallel Scanvenge/Parallel Old, G1

Garbage collection semantics:

  • Parallel (Parallel): Refers to multiple garbage collection threads working in parallel, but at this time the user thread is still in a waiting state.
  • Concurrent: Refers to the simultaneous execution of the user thread and the garbage collection thread (but not necessarily in parallel, and may alternate execution), the user program continues to run, and the garbage collection program runs on another CPU

JVM collector configuration parameters

https://www.cnblogs.com/heqiyoujing/p/11361307.html
jdk1.7 default garbage collector Parallel Scavenge (new generation) + Parallel Old (old generation)
jdk1.8 default garbage collector Parallel Scavenge (new generation) +Parallel Old (old age)
jdk1.9 default garbage collector G1

-XX: parameter configuration collector
-XX:+UseSerialGC Serial + Serial Old
-XX:+UseParNewGC ParNew + Serial Old
-XX:+UseConcMarkSweepGC ParNew + CMS + Serial Old
-XX:+UseParallelGC Parallel Scavenge + Serial Old
-XX:+UseParallelOldGC Parallel Scavenge + Parallel Old

## In UseConcMarkSweepGC mode, Serial Old is used as an alternative to Concurrent Mode Failure of the CMS collector

Concurrent Mode Failure: Because the CMS is executed concurrently with the application during the execution process, if in this process, the application needs to allocate space in the old generation to store objects, and the old generation does not have enough free space at this time, it will Trigger Concurrent Mode Failure, then a Full GC will be performed, and the old generation will be downgraded to use the Serial Old garbage collector. At this time, the execution of all application threads will be suspended

Serial Collector-Copy Algorithm (New Generation)

A single-threaded collector, but its "single-threaded" meaning does not only mean that it will only use one CPU or one collection thread to complete the garbage collection, and more importantly, it must suspend other garbage collections. All worker threads until the end of its collection

The running process of the Serial/Serial Old collector:
Insert picture description here

ParNew-replication algorithm (new generation)

It is a multi-threaded version of the Serial collector. In addition to using multiple threads for garbage collection, the remaining behaviors include all control parameters available to the Serial collector (for example: -XX: SurvivorRatio, -XX: PretenureSizeThreshold, -XX: HandlePromotionFailure, etc.) , Collection algorithm, Stop The World, object allocation rules, recycling strategy, etc. are exactly the same as the Serial collector

The working process of the ParNew/Serial Old collector:
Insert picture description here

Parallel Scanvenge Collector-Copy Algorithm (New Generation)

The goal of the Parallel Scavenge collector is to achieve a controllable throughput (Throughput). The so-called throughput is the ratio of the time used by the CPU to run user code to the total time consumed by the CPU, that is, throughput = time to run user code/(time to run user code + garbage collection time), the virtual machine runs for 100 minutes in total, of which garbage Collecting takes 1 minute, the throughput is 99%

Serial Old Collector-Marking/Organizing (Old Generation)

Parallel Old Collector-Mark/Organize (Old Generation)

CMS Collector-Mark/Remove (Old Generation)

The CMS (Concurrent Mark Sweep) collector is a collector that aims to obtain the shortest recovery pause time.
When using CMS to collect the old generation in JDK1.5, the new generation can only choose one of the ParNew or Serial collectors.
Insert picture description here

The main steps of the CMS collector:

  1. CMS initial mark- Stop The World
    mark the objects that GC Roots can directly associate to

  2. Concurrent mark (CMS concurrent mark)
    for GC Roots Tracing

  3. CMS remark- Stop The World
    corrects the mark record of the part of the object whose mark changes due to the continued operation of the user program during concurrent mark

  4. Concurrent sweep (CMS concurrent sweep)

Disadvantages of CMS collector:

  • The CMS collector is very sensitive to CPU resources. The number of recycling threads started by the CMS by default is (CPU number + 3)/4. When the number of CPUs is greater than 4, the garbage collection thread uses no less than 25% of the CPU resources. When the number of CPUs is insufficient, the CMS responds to the user program. great influence;
  • The CMS collector cannot handle floating garbage, and "Concurrent Mode Failure" may fail and cause another Full GC
  • CMS uses mark-sweep algorithm, which will generate memory fragmentation;

G1 Collector-Marking/Organizing (All Generations)

https://blog.csdn.net/sunjin9418/article/details/79603651
Insert picture description here
Operation steps of G1 collector:

  1. Initial Marking (Initial Marking)
  2. Concurrent Marking
  3. Final Marking
  4. Screening and recycling (Live Data Counting and Evacuation)

Advantages of G1 collector:

  1. Parallel and concurrency: some collectors need to pause the process G1 can still let the user program continue to execute concurrently;
  2. Generational collection: You can manage the entire Java heap without using other collectors;
  3. Spatial integration: use mark-arrangement algorithm, no memory fragmentation;
  4. Predictable pause: In addition to reducing the pause, G1 can also build a predictable pause time model

Guess you like

Origin blog.csdn.net/lewee0215/article/details/112592454