1. The serial collector

  • Serial is a single-threaded garbage collector that collects garbage objects one thread at a time

  • 2. STW occurs when the Serial collector collects garbage

  • 3. Serial garbage collector works on the new generation and adopts the replication algorithm

Serial garbage collection process:

Advantages:

  • 1. Single-core garbage collection, no overhead of thread switching

  • 2. Simple and efficient, it consumes the least extra memory of any garbage collector

  • 3. It is the default new generation garbage collector for HotSpot VIRTUAL machine in client mode

2. ParNew collector

  • 1.ParNew is a multithreaded version of Serial, with other features consistent with Serial

  • 2. Generally used together with the CMS

    1. The ParNew garbage collector works on the new generation and uses a copy algorithm

Garbage collection process:

Advantages:

  • 1. Minimize the time of garbage collection STW under multi-threaded conditions

  • 2. However, the threading environment is not more efficient than serial, which can take advantage of the multi-threaded environment

Applicability. 3

  • 1.Parallel Scavange collector, acting on the New generation, using replication algorithm

  • 2. The purpose of the Parallel Scavange collector is to achieve a manageable throughput

  • 3.Parallel Scavange can set the maximum pause time, -xx :MaxGCPauseMillis

  • 4.Parallel Scavange can set the maximum garbage collection time ratio -xx :GCTimeRatio

Parallel works:

1. Start the Parallel Insane garbage collector

2. Set the maximum heap memory to -xmx

3. Set the maximum pause ratio or the maximum pause time

4. According to the set parameters, the virtual machine will automatically adjust the size of the new generation, the ratio of Eden area to Survivor area, the age value of the object promoted to the old age and other parameters to reach the required pause time or maximum swallowing capacity.

4.Serial Old collector

  • 1. Serial old is the older version of the Serial collector, which works on the older generation and uses the tag collation algorithm

  • 2. The serial old is currently used as a backup in the event of Concurrent Mode Failure in the CMS collector

Serial and Serial Old operation diagram

Parallel Old collector

  • In JDK1.6, a Parallel Old collector was provided to perform old-age garbage collection in place of the Serial Old collector.

  • 2.Parallel Old collector uses a mark-to-compression algorithm, but is also based on Parallel collection and a “stop-the-world” mechanism.

  • 3. In the application scenario of program throughput first, the combination of Parallel collector and Parallel Old collector has a good performance of memory reclamation in Server mode.

  • 4. In Java8, the default is this garbage collector

Parallel Old running process

6.CMS

  • 1.CMS is an old garbage collector for the purpose of obtaining the shortest pause time

  • 2. Using clear marking algorithm, the whole process is divided into four parts: initial marking, concurrent marking, re-marking and concurrent clearing

  • 3. Except for the initial marking and re-marking phases, which require STW to occur, the other two phases are concurrent garbage removal with the application thread

  • When the CMS reaches a certain threshold, space must be reserved for new objects to be allocated. If there is no free memory space, a Concurrent Mode Failture occurs, freezing the execution of user threads and temporarily enabling Serial Old collector to redo Old garbage collection

  • 5.CM S is a collector based on the “mark-sweep” algorithm. When there is too much space debris, the allocation of large objects will cause a lot of trouble. It is often the case that there is still a lot of free space in the old years, but there is not enough contiguous space to allocate the current object, and you have to trigger a Full GC in advance.

  • Initial markup: The initial markup is just a quick markup of an object that GCRoots can associate directly with;

  • Concurrent marking: this is the process of traversing the entire object graph from the directly associated object of GC Roots. This process is time-consuming but does not require the user thread to pause and can be run concurrently with the garbage collection thread.

  • Recallmarking: Corrects the marking record of the part of the object whose mark changed as the user program continued to operate during concurrent marking, preventing the object that was recalled during the concurrent marking phase from being reclaimed

  • Concurrent cleanup: Cleanup removes dead objects judged by the marking phase. Since there is no need to move live objects, this phase can also be concurrent with the user thread

7. The G1 collector

  • 1. The G1 collector is oriented to local collection and region-based memory layout. The G1 collector divides the memory Region into regions of equal size and only reclaims part of the Region at a time.

  • 2. The G1 GC systematically avoids region-wide garbage collection across the entire Java heap. G1 tracks the value of garbage accumulation in each Region (the amount of garbage collection space obtained and the experience value of garbage collection time), maintains a priority list in the background, and collects garbage from the Region with the highest value according to the allowed collection time.

  • 3. In addition to pursuing low pauses, G1 also models predictable pauses, allowing users to explicitly specify that no more than N milliseconds should be spent on garbage collection within a time segment of M milliseconds in length.

  • 4.Region Also has a special Humongous Region for storing large objects. G1 considers large objects as long as the size of a Region exceeds half of its capacity.

  • 5. In order to solve the application problem of cross-region reference objects, G1 avoids full heap scanning in the editing stage and uses memory set to avoid full heap as GC Roots scanning. Each Region of G1 maintains its own memory set, which records Pointers of other regions pointing to it

  • 6.: The CM S collector is implemented by incremental update algorithm, while the G1 collector is implemented by raw snapshot (SATB) algorithm. In addition, the impact of garbage collection on user threads is also shown in the memory allocation of newly created objects in the collection process. New objects will be created continuously if the program continues to run. G1 designs two TAM S(Top at M ARK) for each Region Start) to divide part of Region space for new object allocation during concurrent reclamation. The addresses of newly allocated objects must be above the two pointer positions.

  • 7. STW will occur in the final tag and filter collection

  • Initial M arking: Simply to mark objects that GC Roots can be directly associated with. This phase requires thread pauses, but it is very short, and is done synchronised by borrowing from Minor GC, so the G1 collector actually has no additional pauses at this stage.

  • Concurrent M arking: Performs a reachability analysis of objects in the heap starting with the GC Root, recursively scanning the entire heap object graph for objects to be reclaimed. This phase is time-consuming but can be performed concurrently with user programs. When the object graph scan is complete, it is also necessary to reprocess the objects recorded by SATB that have reference changes at the time of concurrency.

  • Final M arking: Another short pause is made on the user thread to process the last few SATB records that remain after the end of the concurrent phase.

  • Live Data Counting and Evacuation: responsible for updating statistics for regions, ordering the collection value and cost of each Region, creating a collection plan based on the user’s expected pause time, freely selecting any number of regions to form a collection, and then copying the live objects of the part of the Region that decides to be recycled to an empty one Region, and clear all space of the old Region. The operation here involves the movement of the living object, which must suspend the user thread, and is done in parallel by multiple collector threads.