☕[ JVM technical guide] (3) garbage collector of garbage collection system

Time:2021-8-30

☕[ JVM technical guide] (3) garbage collector of garbage collection system

If the collection algorithm is the methodology of memory recycling, the garbage collector is the practitioner of memory recycling

☕[ JVM technical guide] (3) garbage collector of garbage collection system

These seven kinds of garbage collectors work on different generations. If there is a connection between the two collectors, they can be used together. The location of the collector indicates that they belong to the new generation collector or the old generation collector.

Serial collector

Serial collector is a single threaded collector, but its meaning of “single thread” is not only that it only uses one processor or one collection thread to complete garbage collection, but also emphasizes that it must pause all other working threads when garbage collection is carried out until the collection is completed.

The work of “stop the world” is automatically initiated and completed by the virtual machine in the background. When the user is unknown and uncontrollable, all the threads of the user’s normal work are stopped, which is unacceptable for many applications.

☕[ JVM technical guide] (3) garbage collector of garbage collection system

From jdk1.3 to the latest jdk17, the hotspot virtual machine development team has been making continuous efforts to eliminate or reduce the pause of user programs caused by garbage collection, from the serial collector to the parallel collector, to the concurrent mark sweep (CMS) and garbage first (G1) collectors, to the successful Shenandoah and ZGC at the forefront of collectors, The pause time of user thread continues to shorten, but it still can’t be completely eliminated.

Serial is still the default new generation collector for hotspot virtual machine running in client mode. What is its advantage over other collectorsSimple and efficient (compared to the single thread of other collectors), which is the smallest additional memory consumption of all collectors. Since there is no overhead of thread interaction, concentrating on garbage collection can naturally obtain the highest single thread collection efficiency.

Parnew collector

The parnew collector is a multi-threaded parallel version of the serial collector. In addition to using multiple threads for garbage collection, other behaviors include all the 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 policies, etc. are completely consistent with serial collectors.

☕[ JVM technical guide] (3) garbage collector of garbage collection system

Parnew is a hotspot virtual machine running in server-side mode, the important reason is that except for the serial collector, only it can work with the CMS collector at present. The number of collection threads opened by parnew by default is the same as the number of processor cores, which can be accessed through the-20: Use the parallelgcthreads parameter to limit the number of threads for garbage collection.

CMS collector is the first truly concurrent garbage collector in hotspot virtual machine. It enables garbage collection threads and user threads to work at the same time for the first time. Parnew collector is the default new generation collector after CMS is activated. Use the – XX: + useconcmarksweepgc option.

Parallel scavenge collector

On the surface, parallel scavenge is very similar to parnew. It is also a new generation collector, a collector based on tag replication algorithm and a multi-threaded collector that can collect in parallel. However, the goal of the parallel scavenge collector is to achieve a controllable throughput.

Throughput = running user code time / (running user code time + running garbage code time)

Serial old collector

Serial old is an old version of the serial collector. It is also a single threaded collector, using the tag collation algorithm.

☕[ JVM technical guide] (3) garbage collector of garbage collection system

Parallel old collector

Parallel old is an old version of parallel scavenge collector. It supports multi-threaded concurrent collection and is implemented based on tag collation algorithm.

Until the emergence of parallel old, the “throughput first” collector finally has a real combination. When paying attention to throughput or processor resources are scarce, you can give priority to the combination of parallel scavenge and parallel old collector.

☕[ JVM technical guide] (3) garbage collector of garbage collection system

CMS collector

CMS (concurrent mark sweep) collector is a collector aiming at obtaining the shortest recovery pause time. At present, many Java applications focus on the service end of Internet websites or browser based B / s systems. Such applications usually pay more attention to the response speed of services and hope that the system pause time will be as short as possible to bring good interactive experience to users.

☕[ JVM technical guide] (3) garbage collector of garbage collection system

The whole process of CMS collector is divided into four steps:

  1. Initial marking (STW)
  2. Concurrent marking
  3. Retag (STW)
  4. Concurrent cleanup

“Stop the world” is still required in the two steps of initial marking and re marking.

  • Initial markingJust mark the objects that GC roots can be directly associated with, which is very fast;

  • Concurrent markingIt is a process of traversing the entire object graph from the directly related object of GC roots. After all, this process is time-consuming, but does not need to stop the user thread. It can run concurrently with the garbage collection thread;

  • RelabelIt is to correct the mark record (incremental update) of the part of the object whose mark changes due to the continuous operation of the user program during concurrent marking. This stage takes a little longer than the initial stage and a little shorter than the concurrent stage;

  • Concurrent cleanupPhase, clean up and delete the dead objects judged in the marking phase. This phase can also be concurrent with the user thread.

Garbage first collector

Garbage first (G1) collector is a milestone achievement in the development history of garbage collector technology. It creates the design idea of collector for local collection and the memory layout form based on region.

G1 does not adhere to the division of generational regions of fixed size and fixed number, but divides the continuous Java heap into multiple regions of equal size. Each region can play the Eden space, survivor space or elderly space of the new generation as needed.

The collector can heap regions that play different roles and adopt different strategies.

☕[ JVM technical guide] (3) garbage collector of garbage collection system

Four steps of G1 collector operation:

  1. Initial marking: just mark the objects that GC roots can be directly associated with, and modify the value of the TAMs pointer so that when the user thread runs concurrently in the next stage, it can correctly allocate new objects in the available regions. This stage needs to pause the thread, but it takes a short time.

  2. Concurrency marking: start from GC roots to analyze the accessibility of heap objects, recursively scan the object graph of the whole heap to find out the objects to be recycled. This stage takes a long time and can be executed concurrently with the user program. When the object graph is scanned, the objects recorded by SATB with reference changes during concurrency must be reprocessed.

  3. Final mark: make another short pause for the user thread to process the last small amount of SATB records left after the end of the concurrency phase.

  4. Filter recycling: responsible for updating the statistical data of regions, sorting the recycling value and cost of each region, and formulating a recycling plan according to the user’s expected pause time. You can freely select any number of regions to form a recycling set, then copy the living objects of that part of the region to the empty region, and then clean up all the space of the whole old region.

Concurrency of collectors

☕[ JVM technical guide] (3) garbage collector of garbage collection system

Common garbage collector combination parameter setting

  1. -XX:+UseSerialGC = Serial + Serial Old

This option is not available by default. The hotspot virtual opportunity automatically selects the collector according to the calculation and configuration and JDK version

  1. -XX:+UseParNewGC = ParNew + SerialOld

This combination is rarely used (it has been abandoned in some versions)

  1. -XX:+UseConcMarkSweepGC = ParNew + CMS + Serial Old

  2. -20: + useparallelgc = parallel scavenge + parallel old (1.8 default)

  3. -XX:+UseParallelOldGC = Parallel Scavenge + Parallel Old

Differences between useparallelgc and useparalleloldgc:

-XX:+UseParallelGC = Parallel Scavenge + Parallel Old
-XX:+UseParallelOldGC = Parallel Scavenge + Parallel Old
-XX:-UseParallelOldGC = Parallel Scavenge + Serial Old
  1. -XX:+UseG1GC = G1

  2. View the garbage collector used by jdk8 by default

1.8 the default garbage collector is parallel scavenge + parallel old

java +XX:+PrintCommandLineFlags -version
java -XX:+PrintGCDetails -version

☕[ JVM technical guide] (3) garbage collector of garbage collection system

This work adoptsCC agreement, reprint must indicate the author and the link to this article