This is the 31st day of my participation in the August Text Challenge.More challenges in August

WangScaler: A writer with heart.

Declaration: uneducated, if there is a mistake, kindly correct.

Garbage collector is an algorithm for garbage collection. It is an algorithm for garbage collection. It is an algorithm for garbage collection.

  • Serial: a Serial garbage collector that uses a single thread to collect garbage and suspends all threads of the user. Suitable for single-threaded environments.
  • Parallet: a parallel garbage collector that allows multiple garbage collection threads to be processed in parallel, but also suspends all user threads. Suitable for computing/big data scenarios.
  • CMS: Concurrent garbage collector, user thread and garbage collector thread execute simultaneously. Suitable for scenarios where response time is required.
  • G1: G1 garbage collector,
  • ZGC

Serial garbage collector

Single-threaded garbage collectors are one of the oldest garbage collectors in history. One generation is using Serial Copying algorithms and another generation is using Serial Old token compression. Serial Old garbage collectors are generally not used because they have been abandoned.

I can see that when garbage collection takes place, all worker processes, known as STW(stop-the-world), must be suspended because there is no thread interaction, which is simple and efficient but can take a long time.

Parallel garbage collector

Increasing the number of threads compared to the serial garbage collector (the default is equal to the number of cpus, but the number of threads can be changed with the JVM parameter -xx :ParallelGCThreads) also results in STW. The New generation can be ParNew or Parallel insane, and the older generation Parllel Old. ParNew works with CMS.

The Parallel Avenge focuses on throughput (the time user code is run/(the time user code is run + garbage collection)), also known as the throughput-first collector. GC adaptive adjustment mechanism, the size of the dynamic adjustment of the new generation (Xmn), Eden and Survivor ratio (- XX: SurvivorRation), promotion of the old s age (- XX: PretenureSizeThreshold), etc., You can enable the adaptive adjustment mechanism by setting -xx :+UseAdaptiveSizePolicy to obtain the most appropriate pause time (-xx :MaxGCPauseMillis) or the maximum throughput (-xx :GCRatio). Turning on UseParallelGC also turns on the adaptive adjustment mechanism.

Concurrent garbage collector

The CMS concurrent collector is the most common garbage collector (pre-G1) and is perfect for the Internet or B/S servers. It only collects old garbage, using the tag cleanup algorithm.

There are four main steps

  • 1. Initial tag: mark reachable alive objects (i.e. objects reachable by GC-root).
  • 2. Concurrent marking: GC Roots Tracing, marking the objects changed after the initial marking (promotion of the new generation to the old generation, assignment of the old generation objects, updating the reference relationship of the old generation objects, etc.) and re-marking the Card where the object is located as Dirty.
  • 3. Re-mark: Judge and mark the objects generated during marking.
  • 4. Concurrent cleanup: Clear unreachable objects.

As you can see from the figure, both initial marking and re-marking result in the termination of the user thread, the STW. The concurrent marking and concurrent cleaning process can be carried out simultaneously with the user thread, so the pause time is reduced. It is a collector with the goal of obtaining the shortest collection pause time.

A concurrent mode failure occurs during the concurrent marking phase due to floating garbage, resulting in a Full GC. Because the concurrent garbage collector uses a marker clean algorithm, it inevitably generates space debris, and when large objects claim space, it often causes Full GC in advance.

G1 collector

The default service-oriented garbage collector from JDK7 to JDK9 has greatly reduced the pause time for garbage collection and added a predictive mechanism that allows users to specify a desired pause time (-xx :MaxGCPauseMillis). Change the original Eden and Survivor and Tenured regions into regions of the same size (1m to 32m each, powers of 2 set to -xx :G1HeapRegionSize) The default is 2048 partitions), so on the whole, the new area and the old area are no longer distinct, but become some regions are new area and some regions are old area. The G1 collector uses the mark compression algorithm as a whole and the replication method locally.

The new partition will look like the one above, where Humongous is used to store large objects, and if one Humongous is not enough, multiple partitions will be used for storage.

45 to GC, the default heap usage by – XX: InitiatingHeapOccupancyPercent modified – XX: ConcGcThreads modified GC thread number, – XX: G1ReservePercent setting the reserved free space ratio of memory, the default is 10%.

other

You can check the default garbage collector by running the Java -xx :+PrintCommandLineFlags -version command. For more JVM commands or for a different garbage collector, refer to the JVM parameters common in previous articles. The default garbage collection period for java8 is the parallel garbage collector. UseSerialGC (Serial+Serial Old); UseParNewGC (ParNew+Serial Old); Not recommended), UseParallelGC (Parallel Avenge +Parllel Old), UseParallelOldGC (Parallel avenge+Parllel l Old), UseConcMarkSweepGC (ParNew+CMS+Serial Old), because CMS and user threads running at the same time will also consume heap memory, when the heap memory is insufficient, If the CMS fails, the Serial Old collector will be used for collection), UseG1GC, and the abandoned parameter UseSerilalOldGC(Serial Old).

Here’s how the collector works in combination, with the red line showing the way that is no longer recommended after java8.

As can be seen from the figure above, Serial garbage collector and ParNew garbage collector are used in conjunction with CMS collector.

Usage Scenarios:

parameter scenario
UseSerialGC Single CPU or small memory standalone program
UseParallelGC/UseParallelOldGC Multi-cpu, large throughput
UseParNewGC/UseConcMarkSweepGC Pursuit of response speed

conclusion

  • G1 is a garbage collector for both the old and new generations.
  • Serial, ParNew, and Parallel Scavenge are the next-generation garbage collectors.
  • 3, Serial Old, Parllel Old, CMS three collectors are Old garbage collectors.
  • UseParallelGC and UseParallelOldGC are the new generation using the Parallel Avenge, and Parllel Old. The difference between the two is that the latter prefers the Old age to use the parallel garbage collection period, because only Parllel Old of the Old age is replaced with SerialOld when -useParalleloldgc is used, while the new generation remains unchanged.
  • The advantages of G1 over CMS are that G1 has no memory fragmentation and can control pause times.

Come all come, click “like” and then go!

Follow WangScaler and wish you a promotion, a raise and no bucket!