If you don’t say more, the liver will die.

Garbage collector

The garbage collection algorithm is a set of rules that govern what objects can be collected. The garbage collector can be seen as the enforcer of this specification. Garbage collection of recyclable objects frees memory. Let’s take a look at the characteristics of each garbage collector. Let’s start with a picture

The figure shows a total of seven garbage collectors working on different generations. The lines indicate that garbage collectors can be used in combination with different generations. The next seven types of garbage collectors are described in detail.

Serial collector

Serial collector is the most basic collector, acting on the new generation, using the replication algorithm, it is a single-threaded collector. It is important to note that by single threading it does not simply mean using a CPU or a collection thread to do garbage collection. Most importantly, it suspends all other normal threads that are working while garbage is collected. ** Until garbage collection is complete, also known as “Stop The World”.

In common sense, you say, “I’m going to start cleaning out the garbage, so you all stop and don’t do anything, and when I’m done, you can do your own work.” Look at the map

With Serial collector and Serial collector work marked along The way, combined with The diagram, it is easy to understand “Stop The World”, The garbage collection thread starts, and then The “red light” is displayed at The entrance, at which point all user threads pause and wait. When the garbage collection thread has finished collecting, the “green light” lights up and all user threads start working normally. Think about traffic lights.

Since it is single-threaded, there is no overhead of thread interaction, and the efficiency is relatively high. It is still the default generation collector for virtual machines running in Client mode.

ParNew collector

The ParNew collector is essentially a multithreaded version of the Serial collector. In addition to garbage collection using multiple threads, The Serial collector is available with control parameters, collection algorithms, Stop The World, object allocation rules, collection policies, and more.

The ParNew collector is the default generation collector when the virtual machine is running in Server mode. Compared to the Serial collector, because it is multithreaded, it naturally increases the overhead on thread interactions. So the performance is a little bit worse than the Serial collector. In the case of multiple cpus, the number of threads for garbage collection can be set with the ** -xx :ParallelGCTherads parameter **.

Parallel avenge

The Parallel Avenge is also a new generation garbage collector that uses replication algorithms. It’s also a multithreaded processor. In terms of these features, the ParNew collector seems to be the same. On closer inspection, the Parallel Avenge collector is more concerned with achieving a controlled throughput.

Throughput: The ratio of the time the CPU spent running user code to the total CPU consumption, i.e. Throughput = user code runtime/(user code runtime + garbage collection time)

For example, if the total CPU consumption time is 100, the garbage collection time is 1, and the user runs code time is 99, the throughput is 99%

As mentioned earlier, garbage collection stops The World, and The shorter The pause, The faster The program responds. High throughput can effectively improve the efficiency of CPU utilization, allowing more time to run user code, thus faster completion of the program’s computational tasks.

Two parameters are used to set the throughput of the Parallel Insane collector. Control the maximum pause time (-xx :MaxGCPauseMillis) and directly specify the throughput size (-xx :GCTimeRatio).

The maximum pause time (-xx :MaxGCPauseMillis) is in milliseconds. It is worth noting that the shorter the GC pause times, the more space and throughput of the new generation are sacrificed. For example: the new generation from 500M to 300M, 300 must be faster than 500 collection, the original may be a garbage collection every 10 seconds, each collection pause time of 100 milliseconds, may become not 5 seconds to collect once, pause time to 70 milliseconds. So it looks like the pause time is shorter, but the space is smaller, which means the collection frequency is higher. Going from 10 seconds to 5 seconds means that each collection is paused and the user code runs for less time. Throughput is reduced. So from a comprehensive perspective, excessively short pause times do not mean that they will get good results.

The Parallel Avenge collector is also known as the “through-first” collector because of its affinity for throughput. In addition to the above two arguments, there is another argument ** -xx :+UseAdaptiveSizePolicy**. This is a switch parameter. After setting this switch parameter, you do not need to manually set details such as the size of the new generation and the ratio of Eden to Survivor regions. The virtual machine automatically adjusts these parameters to achieve the optimal maximum pause time or throughput based on actual performance. This type of adjustment is also known as GC adaptive strategy. It is also different from the ParNew collector.

Serial Old collector

The Serial Old collector is an older version of the Serial collector. Unlike Serial, it uses a mark-collation algorithm. It is mainly used in the Client mode of virtual machines.

In Server mode, it has two main functions:

  1. Prior to JDK1.5, it was used with a Parallel collector.
  2. As a fallback to the CMS collector in the event of a Concurrent Mode Failure in garbage collection

Parallel Old collector

Also an older version of the Parallel Avenge. The mark-collation algorithm is also used. Be distinguished from the Parallel avenge in the generation of the application. Prior to JDK1.6, the Parallel exploitoring was only used with Serial Old and did not capitalize on the through-first trend. The Parallel Old Exploiter can be used as a companion to the Parallel Insane. Achieve optimal “throughput first”.

So Parallel combination is preferred in scenarios where throughput and CPU resources are important.

CMS collector

The CMS collector (Concurrent Mark Swap) is a garbage collector whose goal is to obtain the shortest collection time. Short garbage collection times mean short pause times and relatively high system response times.

Compared to the previous four garbage collectors, it is relatively complex. The first thing to be clear about is that it uses the **” mark-erase algorithm “**. There are four main steps:

  • Initial mark: Mark objects to which GC Roots can be directly associated and Stop The World, but for a short time
  • Concurrent markup: The process of GC Roots Tracing,
  • Remarking: Corrects The marking record of The part of The object that was marked during The concurrent marking phase because The user program continued to operate. Stop The World, longer than The initial marking, but shorter than The concurrent marking.
  • Concurrent cleanup: Clears objects

This process looks a bit like the two-token process of the reachability algorithm. Concurrent tagging and concurrent cleanup processes can be performed with user threads in a concurrent relationship. As shown in figure:

Concurrency, short pause time

  1. It is very sensitive to CPU resources. The default number of concurrent threads in the CMS is (NUMBER of cpus +3) /4, which means that when the number of cpus is 4, 25% of THE CPU resources will be used for garbage collection. Comparatively, the CPU resources allocated to the number of user threads will not be sufficient, which will affect the efficiency of system execution.

  2. Unable to handle floating garbage, a “Concurrent Mode Failure” may occur, resulting in a Full GC. When a “Concurrent Mode Failure” occurs, the Serial Old collector will be activated to redo the Old garbage collection, resulting in long pauses.

    Concurrent Mode Failure

    Floating garbage

    CMS clear concurrent threads are threads execute concurrently with users, with the operation of the program will continue to have new waste, waste may appear in this part of the labeling, this means that the CMS cannot clear them in time to collect the process, can only be clear again the next time the GC, this part of the garbage objects is called floating garbage.

    Since user threads are running normally during the concurrent cleanup phase, this means that new objects are constantly being created, which requires sufficient internal slave space to be allocated to user threads. The CMS collector cannot wait until the ages are filled as other collectors do. Due to floating garbage, the space reserved for user threads may not be sufficient. A “Concurrent Mode Failure” occurs, which triggers a backup plan for the virtual machine.

  3. Because of the “mark-sweep algorithm “, this means that garbage collection generates memory fragmentation.

G1 collector

The G1 collector is one of the most advanced technologies in the virtual machine garbage collector. It is a service-oriented garbage collector. Compared to other garbage collectors, it has the following characteristics:

  • Parallelism and concurrency: G1 can take full advantage of The hardware advantages of multi-CPU, multi-core environment, using multiple cpus to shorten The stop-the-world pause time, some of The other collectors originally need to pause Java threads to perform GC actions, G1 collector can still make Java programs continue to execute through The way of concurrency.
  • Generational collection: As with other collectors, the concept of generational collection still exists.
  • Spatial consolidation: Unlike the “mark-clean” of the CMS collector, the “mark-tidy algorithm” is used at large, but the “copy algorithm” (between the two Regins) is used at small scales, and in general, memory fragmentation is not generated.
  • Predictable pauses: this is another big advantage of G1 relative to CMS, reduce the pause time is the common concern of G1 and CMS, but G1 in addition to the pursuit of low pause, also can establish predictable pauses model, can let the user specify in a length of M segment within milliseconds, time on garbage collection may not consume more than N milliseconds.

Unlike other collectors, which collect mainly young and old objects, G1 has a very different memory layout for the heap. It still has the concept of young and old, but it is not physically isolated like other collectors. It divides the entire heap into regions. New generation and old generation are collections of regions (regions do not need to be continuous).

conclusion

The G1 collector is relatively complex, so I will write a separate article to document the learning process of the G1 collector. This time, I will briefly introduce the following.


Not afraid of road evil not afraid of rain drench, heart a word dare to face my dream, willing to be foolish. > – < silly people