Talk about the garbage collector of the JVM

Serial: Refers to the garbage collector and user threads alternately, which means that the user thread needs to suspend work when the garbage collector is executed Parallel
: refers to the simultaneous execution of the garbage collector and user threads, except for cms and G1 Other garbage collectors are executed in a serial manner

insert image description here

1.Serial collector

Serial is translated as serial, that is to say, it is executed in a serial manner. It is a single-threaded collector and only uses one thread for garbage collection. The advantage is that it is
simple and efficient. For a single cpu environment, there is no thread switching Overhead, high collection rate
It is the default garbage collector in client (client) mode

insert image description here

2. Parnew collector

It is a multi-threaded version of the serial collector, and it is the preferred new generation collector for virtual machines in server mode. In addition to performance reasons, the main reason is that in addition to the serial collector, only it can work with the cms collector. The number of threads enabled by
default Same as the number of cpus, you can set the number of threads with the -xx:parallelGCThreads parameter

insert image description here

3. Parallel Scavenge collector

Like parnew, it is a multi-threaded collector.
Other collectors focus on shortening the pause time of user threads during garbage collection as much as possible, and his goal is to achieve a controllable throughput. It is called a throughput-first collector. Here Throughput is worth the ratio of the time the CPU spends on running user code to the total time.
The shorter the pause time, the more suitable for applications that need to interact with users. Good response speed can improve user experience, and high throughput can efficiently use CPU time. , to complete the task as soon as possible and shorten the pause time is in exchange for sacrificing throughput and new generation space: the new generation space becomes smaller, and garbage collection becomes frequent, resulting in a decrease in throughput. You can turn on the GC adaptive adjustment strategy (GC Ergonomics) through a switch parameter, so you don't need to manually specify the size of the new generation (-Xmn), the ratio of Eden and Survivor areas, and the age of objects in the old generation. The virtual machine collects performance monitoring information according to the current system operating conditions, and dynamically adjusts these parameters to provide the most suitable pause time or maximum throughput.

4.Serial Old

It is the old version of the serial collector, and it is also used for the virtual machine in the client mode. If it is in the server mode, it has two major uses:

  • Used with the parallel collector in jdk1.5 and previous versions (before parallel old was born)
  • As a backup plan for the cms collector, it is used when Concurrent Mode Failure occurs in concurrent collection

5.parallel old collector

It is an old version of the parallel scavenge collector.
In occasions that focus on throughput and are sensitive to cpu resources, parallel scavenge plus parallel old collector is given priority.

insert image description here

6.cms

insert image description here

Concurrent Mark Sweep, Mark Sweep refers to the mark-clear algorithm, which
is divided into the following four processes:

  • Initial marking: just mark the objects directly related to the GC root, which is fast and needs to be paused
  • Concurrent marking: the process of performing GC root tracing, which takes the longest time in the entire recycling process and does not require a pause
  • Remarking: A pause is required in order to correct the mark record for that part of the object whose mark has changed due to the continuation of the user program during concurrent marking.
  • Concurrent clearing: no pauses are required.

During the longest concurrent mark and sweep process in the entire process, the collector thread can work with the user thread without stalling. Has the following disadvantages: Low throughput: Low pause times come at the expense of throughput, resulting in insufficient CPU utilization. Unable to process floating garbage, Concurrent Mode Failure may occur. Floating garbage refers to the garbage generated during the concurrent cleanup phase due to the continuous running of user threads. This part of garbage can only be recycled in the next GC. Due to the existence of floating garbage, a part of memory needs to be reserved, which means that CMS collection cannot wait for the old generation to be full before recycling like other collectors. If the reserved memory is not enough to store floating garbage, Concurrent Mode Failure will occur, and the virtual machine will temporarily enable Serial Old to replace CMS. The space fragmentation caused by the mark-clear algorithm often has space remaining in the old age, but cannot find a large enough contiguous space to allocate the current object, and has to trigger a Full GC in advance.

7. G1 Collector

G1 (Garbage-First), which is a garbage collector for server-side applications, has good performance in scenarios with multiple CPUs and large memory. The mission entrusted by the HotSpot development team is to replace the CMS collector in the future. The heap is divided into the new generation and the old generation. Other collectors collect the entire new generation or the old generation, while G1 can directly recycle the new generation and the old generation together.

G1 divides the heap into multiple independent regions (Regions) of equal size, and the new generation and the old generation are no longer physically isolated


By introducing the concept of Region, the original whole memory space is divided into multiple small spaces, so that each small space can be garbage collected separately. This method of partitioning brings a lot of flexibility, making predictable pause time models possible. By recording the garbage collection time of each Region and the space obtained by recycling (these two values ​​​​are obtained through past recycling experience), and maintaining a priority list, each time according to the allowed collection time, the Region with the highest value is preferentially recycled. Each Region has a Remembered Set, which is used to record the Region where the reference object of the Region object is located. By using the Remembered Set, a full heap scan can be avoided when doing reachability analysis.

If the operation of maintaining the Remembered Set is not counted, the operation of the G1 collector can be roughly divided into the following steps: Initial mark Concurrent mark Final mark: In order to correct the part of the mark that changes due to the continued operation of the user program during the concurrent mark Record, the virtual machine records the object changes during this period in the Remembered Set Logs of the thread, and the final marking stage needs to merge the data of the Remembered Set Logs into the Remembered Set. This phase requires thread stalling, but can be executed in parallel. Screening recovery: first sort the recovery value and cost in each Region, and formulate a recovery plan according to the user's expected GC pause time. In fact, this stage can also be executed concurrently with the user program, but because only a part of the Region is recycled, the time is controllable by the user, and pausing the user thread will greatly improve the collection efficiency. It has the following characteristics: Spatial integration: Overall, it is a collector based on the "mark-sort" algorithm, and locally (between two Regions), it is based on the "copy" algorithm, which means that it will not Generate memory space fragmentation. Predictable Pause: It allows users to specify that within a time segment of M milliseconds, the time spent on GC should not exceed N milliseconds.


Guess you like

Origin blog.csdn.net/qq_54796785/article/details/128769569