Java Classic Garbage Collector

1.Serial collector

The Serial collector is the most basic and oldest collector. It is a new generation collector . The new generation collection uses the "mark-copy algorithm"; its working method is " single thread ". The single thread here not only means that it only uses one processor or one collection thread to complete the garbage collection work, but more importantly, it emphasizes that it must suspend all other working threads, including user threads, during garbage collection. It's collection over. So, "Stop The World" (STW) is not called for nothing.

2. ParNew collector

The ParNew collector is actually a multi-threaded parallel version of the Serial collector , and it can be seen from the name that it is also a new generation collector.

But the multi-threaded parallelism here does not mean that it can be without SWT. Let me explain the concept of collector concurrency and parallelism:

  • Parallel (Parallel): Parallel describes the relationship between multiple garbage collector threads, indicating that there are multiple such threads working together at the same time. Usually, the user thread is in a waiting state by default at this time.
  • Concurrent: Concurrent describes the relationship between garbage collector threads and user threads, indicating that both garbage collector threads and user threads are running at the same time. Since the user thread is not frozen, the program can still respond to service requests, but because the garbage collector thread occupies a part of system resources, the processing throughput of the application program will be affected to a certain extent.
  • —Excerpt from "In-depth understanding of Java virtual machine"

3. Parallel Scavenge collector

The Parallel Scavenge collector is also a new generation collector, which is also implemented based on the " mark-copy algorithm ", and its working method is also a multi-threaded collector that collects in parallel . But it has one of the biggest features: try to achieve a controllable throughput.

For example, to complete a task, user code plus garbage collection takes a total of 100 seconds, of which garbage collection takes 1 second, then the throughput is 99%.

4.Serial Old collector

The Serial Old collector is an old-age version of the Serial collector . It is the same single thread , but the difference is that it is implemented based on the " mark-sort algorithm ".

5. Parallel Old Collector

The Parallel Old collector is an old-age version of the Parallel Scavenge collector , which supports multi-threaded concurrent collection and is implemented based on the " mark-sort algorithm ".

6. CMS collector

The CMS (Concurrent Mark Sweep) collector is a collector based on the " Mark-Sweep Algorithm " with the goal of obtaining the shortest recovery pause time (shortening the STW time). Its operation is more complicated. The whole process is divided into the following four step:

(1) Initial marking : mark the following objects that GC Roots can directly relate to, the speed is very fast, and "STW" is required

(2) Concurrent marking : Traversing the entire object graph from the directly associated objects of GC Roots, the speed is relatively slow, but "STW" is not required

(3) Re-marking : mark those objects that may be re-abandoned by the user during the concurrent marking period, the speed is slightly longer than the initial marking, and "STW" is required

(4) Concurrent cleanup : Clean up marked dead objects. Since there is no need to move surviving objects, it can be executed concurrently without "STW"

Since the time for initial marking and re-marking is very short, the CMS collector can be understood as the process of memory recovery being executed concurrently with user threads .

7. Garbage First collector

The Garbage First (G1 for short) collector is a collector with a " pause time model ", and the key to realizing this model is the Region -based heap memory layout. This collector is just like T1 (faker), very strong, and it is the strongest among the models currently introduced .

The pause time model means that it can support the goal of specifying that within a period of M milliseconds, the time spent on garbage collection has a high probability of not exceeding N milliseconds. We can understand it as: I can finally set the garbage collection time I want.

Region : Divide the continuous Java heap into multiple independent regions of equal size, and each region is a Region. Its extraordinary feature is that each Region can play different roles according to needs: it can be Eden space, Survivor space, or old generation space. It is worth mentioning that there is also a special Humongous area in the Region, which is specially used to store large objects (objects that occupy half the size of a Region), and the G1 collector generally treats the objects in this area as the old generation.

insert image description here

The operation process of the G1 collector is divided into the following four steps:

1. Initial marking : Marking the objects that GC Roots can directly relate to requires a short pause and takes a short time.

2. Concurrent marking : It takes a long time to analyze the reachability of objects in the heap starting from the GC Root and find the objects to be recycled, but it is executed concurrently with user threads, and there is no "STW". After the scanning of the object graph is completed, it is necessary to reprocess the objects whose references have changed during the concurrency just now.

3. Final mark : Make another short pause on the user thread to process the last few SATB records left after the end of the concurrent phase.

4. Screening and recycling : first update the statistical data of the Region, sort the recycling value and cost of each Region, select multiple Regions to be recycled according to the time specified by the user (the pause time model is shown here), and collect the The surviving objects are copied to the empty Region, and then all the space in the old Region is cleaned up. This involves moving surviving objects, so the user thread needs to be suspended, and multiple collector threads are completed in parallel.

It can be seen that in addition to concurrent marking, the G1 collector needs to suspend user threads at other stages. It is not purely pursuing low latency, but to obtain the highest possible throughput under controllable latency, so it can afford "full The responsibilities and expectations of the Feature Collector.

Guess you like

Origin blog.csdn.net/m0_52373742/article/details/121321890