“This is the fifth day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

Classic garbage collector

If collection algorithms are the methodology of memory collection, then the garbage collector is the practice of memory collection. Because there is no established standard, garbage collectors vary from vendor to vendor, and various parameters are provided for users to combine collectors for different generations according to their own applications and requirements.

Because there is no one-size-fits-all garbage collector, we need to choose the collector that is most appropriate for the scenario.

Serial collector

The Serial collector is the most basic and oldest collector and was the only choice for the new generation of HotSpot virtual machine collectors prior to JDK1.3.1. The collector must suspend all other worker threads while collecting until it finishes collecting. The task of suspending all other worker threads is to stop all the normal worker threads of the user under the circumstances that the user is unknown and uncontrollable.

However, it is still the default new generation collector in HotSpot client mode, because it is simple and efficient, its extra memory consumption is relatively small, and its single thread collection is efficient because there is no overhead of thread crossing. In addition, the memory used by the new generation objects of desktop applications is not very large, and the garbage collection pause time can be controlled to tens of milliseconds.

ParNew collector

ParNew is essentially a multithreaded version of Serial, a new generation garbage collector that is based on a mark-copy algorithm for garbage collection and is essentially the same except for the simultaneous use of multiple threads for garbage collection, and the two collectors share a considerable amount of code. The collector is one of a number of HotSpot virtual machines running in server mode, mainly because it is the only one that can be used with the CMS collector.

Due to the higher performance of the G1 garbage collector, the ParNew collector only works in conjunction with the CMS collector from JDK9 onwards, and is no longer officially recommended collector solution in server-side mode.

Parallel avenge

The Parallel Collector is a new generation collector, and is also a multithreaded garbage collector that can collect in Parallel, based on the mark-copy algorithm.

This garbage collector is specific in that it focuses on throughput, not the pause times of user threads. Therefore the Parallel Scanvenge garbage collector is also known as a throughput first collector. In addition, the adaptive adjustment strategy can be enabled to dynamically adjust the size of the new generation, the size of Eden and Survivor area, and the size of the promoted old age object, and provide the most appropriate pause time or maximum throughput.

conclusion

Today, we will introduce some of the classic new generation garbage collectors, and then we will introduce some other classic Old garbage collectors (Serial Old, Parallel Old, CMS).