This is the 25th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Garbage collector

Garbage collection algorithms are like interfaces in Java, and garbage collectors are implementations of interfaces. Therefore, different vendors and different versions of virtual machines implement different ways. Even a big difference.

The following figure shows the garbage collector in a common HotSpot VIRTUAL machine.

The New generation is the Serial, ParNew, Parallel Insane, and the older generation is the CMS, MSC, Parallel Old insane.

5.1 Serial Collector

Serial is the most basic, oldest, and simplest collector. It is a single-threaded collector. When it begins garbage collection, all other worker threads must be suspended until the collection is complete. The Serial collector has The advantage of being far more efficient than other collectors in a single-CPU environment because it does garbage collection without The overhead of thread operations, although The user experience of “Stop The World” occurs during garbage collection.

5.2 ParNew collector

The ParNew collector is a parallel version of the Serial collector, which differs from Serial in that garbage collection occurs in parallel during STW. It is the collector of choice for the new generation in most Server models, not least because only ParNew and Serial are currently available with older CMS collectors. So if you have an older collector and you choose CMS for a new generation collector you have to choose between the two.

Note that in a single CPC environment, the ParNew collector is certainly not as efficient as the Serial collector because of the overhead of interacting with threads. Even two cpus are not 100% guaranteed to be higher than Serial. Of course, as the number of cpus increases, the performance of the ParNew collector gets better and better.

5.3 Parallel Scavenge

The Parallel Collector is a new generation collector that uses a replication algorithm and collects in Parallel. The function is very similar to ParNew. While other collectors focus on minimizing the STW insane, the Goal of the Parallel Scavenge collector is to control the percentage of time the STW consumes. This percentage is called throughput. This is the ratio of the execution time of user code to the total execution time of the CPU. For example, if the JVM runs for a total of 100 minutes and GC takes 2 minutes, the throughput is 98% (98 /(98+2)). Using the Parallel Insane collector, you can set the size of the throughput using the “-xx :GCTimeRatio” parameter.

The value of the GCTimeRatio parameter is an integer value of a (0,100) closed interval, meaning the percentage of garbage collection time. Equivalent to the reciprocal of throughput. The default value is 99, which allows up to 1% (1 /(1+99) of the time for garbage collection.

You can also set the cycle of each GC with the “-xx :MaxGCPauseMillis” parameter, and the collector will try to ensure that the garbage collection time does not exceed this value. However, do not assume that setting this value slightly smaller will make the GC time faster. The pause time of GC is gained by sacrificing space for the new generation. The smaller space in the new generation may lead to an increase in the number of GC, and the overall throughput may decrease rather than increase.

5.4 Serial Old Collector

The Serial Old collector is an older version of the Serial collector, which is also single-threaded and uses the mark-collation algorithm. It has two main uses, one for the Client side. The other is a backup for the CMS collector on the Server side (see CMS Collector).

5.5 Parallel Old Collector

Similarly, the Parallel Old is an older version of the Parallel Avenge collector that uses multithreading and a “mark-and-collate” algorithm. To be insane, if the older generation uses Parallel Old, the newer generation can only use the Parallel Insane. This combination can be considered in some throughput first scenarios.