Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the JVM garbage collectors

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what are the JVM garbage collectors". In the daily operation, I believe many people have doubts about the problems with the JVM garbage collectors. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what are the JVM garbage collectors?" Next, please follow the editor to study!

Seven commonly used collectors, the scope of which is shown in the figure

Serial, ParNew, Parallel Scavenge for Cenozoic era

CMS, Serial Old and Paralled Old are used in the old age. And they are used in relatively fixed combinations with each other (the specific combination relationship is shown above). G1 is a stand-alone collector that does not depend on the other six collectors. ZGC is the experimental collector of JDK 11 at present.

Let's take a look at the characteristics of each collector.

Serial collector

Serial, which performs garbage collection in a single thread. When garbage collection needs to be performed, the program pauses all work at hand, and then performs garbage collection in a single thread.

Because the characteristic of the new generation is the low survival rate of the object, the collection algorithm uses the replication algorithm to copy the new generation of surviving objects to the old age, the content of replication is not much, and the performance is better.

The advantage of single thread is to reduce context switching and reduce the overhead of system resources. But the disadvantage of this approach is also obvious, in the process of GC, the execution of the program will be suspended. If GC does not occur frequently, this may be a good choice, otherwise it will affect the execution performance of the program. For the new generation, the area is relatively small and the pause time is short, so it is more used.

ParNew collector

ParNew is also used in the new generation, is a multithreaded version of Serial, and is exactly the same as Serial in terms of parameters and algorithms (also replication algorithms).

Par is the abbreviation of Parallel, but its parallelism only refers to the collection of multi-thread parallelism, not that the collection and the original program can be carried out in parallel. ParNew also needs to pause all the work of the program, and then perform garbage collection with multiple threads.

Because it is multithreaded, ParNew is usually better than Serial in multi-CPU. However, if it is a single CPU, the performance will be even worse because of thread switching.

Parallel Scavenge collector

The new generation of collectors also use replication algorithms and parallel multithreaded collections. Unlike ParNew, it focuses on the throughput of garbage collection.

Throughput here refers to the ratio of total time to garbage collection time. The higher this proportion, the smaller the proportion of garbage collection in the whole program.

The Parallel Scavenge collector provides two parameters that control the execution of garbage collection:

-XX:MaxGCPauseMillis, maximum garbage collection pause time. The principle of this parameter is that space is exchanged for time, and the collector will control the area size of the new generation, so as to ensure that the recovery is less than the maximum pause time as far as possible. To put it simply, the smaller the area of recycling, the less time it takes.

So this parameter is not set as small as possible. If the setting is too small, the space of the new generation will be too small, thus triggering GC more frequently.

-XX:GCTimeRatio, the percentage of garbage collection time to total time. This is the reciprocal of throughput, and the principle is the same as MaxGCPauseMillis.

Because the Parallel Scavenge collector is concerned with throughput, when setting the above parameters, you do not want to set the size of each region (Cenozoic, older, etc.). You can enable the * *-XX:UseAdaptiveSizePolicy** parameter to allow JVM to monitor the performance of the collection and dynamically adjust the size parameters of these areas.

Serial Old collector

The old collector, like Serial, is single-threaded, except that the algorithm uses tag-collation (Mark-Compact).

Because the survival rate of objects in the old years is high, if you still use the replication algorithm, you need to copy more content, the performance is poor. And in extreme cases, when the survival is 100%, there is no way to use the replication algorithm. So you need to use Mark-Compact to effectively avoid these problems.

Parallel Old collector

The old collector is the old version of Parallel Scavenge. The algorithm is replaced by Mark-Compact.

CMS collector

CMS,Concurrent Mark Sweep is also the collector of the old days. It focuses on the shortest pause time (low pause) for garbage collection, which is more applicable in the case of infrequent GC in the old days.

Concurrent is used in the naming, not parallel, indicating that the collector has the ability to run concurrently with work execution. MS indicates that the algorithm uses the Mark Sweep algorithm.

Let's take a look at how it works. The whole process of CMS is more complex than the previous collector, which is divided into four steps:

Initial markup (initial mark), single-threaded execution, requires "Stop The World", but only marks the directly associated reachable objects of GC Roots, because the direct associated objects are relatively small, so the speed here is very fast.

Concurrent markup (concurrent mark), for the initial tagged object marked by the initial tagging process, is marked concurrently so that other threads can continue to work. It takes a long time here, but there is no pause.

Remark, as new garbage may also be generated during the concurrent marking process, the newly generated garbage needs to be relabeled at this time. The parallel markup is executed here and is not concurrent with the user thread, so it is still "Stop The World" and takes a little longer than the initial time.

Concurrent cleanup (concurrent sweep), concurrently cleaning up previously marked garbage. Other user threads can still work without pause.

Since the most time-consuming concurrency marking and concurrent cleanup phases do not need to be paused, the overall recovery is low-pause.

Due to the above characteristics of CMS, the shortcomings are also obvious.

The Mark Sweep algorithm will result in more memory fragments.

The concurrency ability of CMS depends on CPU resources, so when the number of CPU is small and CPU resources are tight, the performance is poor.

In the concurrent cleanup phase, the user thread is still running, so new garbage will still be generated. The garbage at this stage will not be collected in this GC, but will be placed next time. So GC cannot wait for GC to run out of memory, which can lead to insufficient space for user threads to take advantage of when concurrent cleanup occurs. So there is a waste of memory space reserved for user threads.

Some people may think that since Mark Sweep can cause memory fragmentation, why not replace the algorithm with Mark Compact?

The answer is actually very simple, because how can the memory used by the original user thread be used when the memory is cleared concurrently and Compact is used to organize the memory? To ensure that the user thread can continue to execute, as long as the resources it runs are not affected. Mark Compact is more suitable for use in scenarios such as "Stop the World".

G1 collector

G1Jing garrison First, which was officially launched in JDK 1.7, was the cutting-edge garbage collector at that time. G1 can be said to be the ultimate improved version of CMS, solving the problem of CMS memory fragmentation and more memory space. Although the process is similar to CMS, the underlying principle is completely different.

High efficiency is given priority. G1 will predict the standstill time of garbage collection, the principle is to calculate the efficiency of the old object, giving priority to the object with the greatest benefit.

Differences in heap memory structure. The previous generation of collectors is divided into the new generation, the old age, the lasting generation and so on.

On the other hand, G1 divides the memory into several Region regions of the same size, each Region has its own generational properties, but these generations do not need to be continuous.

Such a partition can effectively avoid the problem of memory fragmentation.

But this will also lead to a new problem, that is, generation-by-generation memory is not contiguous, resulting in a full scan to find out where the reference memory is when GC searches for junk objects.

To solve this problem, G1 maintains a Remembered Set for each Region to record object references. When GC occurs, search according to the citation of Remembered Set.

Two GC modes:

Young GC, which focuses on the Region of all younger generations, controls the recovery time of GC by controlling the number of Region collected by the younger generation.

Mixed GC, which focuses on the Region of all younger generations, plus several older Region that calculate the maximum returns through forecasts.

The overall implementation process:

The initial tag (initial mark), which marks the objects that are directly associated with reachability from the GC Root. STW (Stop the World) execution.

Concurrent markup (concurrent marking), which marks the object of the initial tag concurrently, while the user thread can still execute.

The final tag (Remark), STW, the garbage generated by the tag and the concurrent tag process.

Filter recycling (Live Data Counting And Evacuation), evaluate marked garbage, and collect garbage according to GC mode. STW execution.

At the Region level, the overall algorithm is biased towards Mark-Compact. Because it is Compact, it will affect the user thread execution, so the recycling phase needs to be executed by STW.

Amazing ZGC

In JDK 11, an experimental ZGC is added. It takes less than 2 milliseconds to recycle on average. It is a collector with low pause and high concurrency.

ZGC is executed concurrently almost everywhere, except that the initial tag is STW. So the pause time is almost spent on the initial tag, which is actually very small. So how can other phases be executed concurrently?

ZGC mainly adds two new technologies, one is shaded pointer Colored Pointer, and the other is read barrier Load Barrier.

Shading pointer Colored Pointer

ZGC uses several bits in the 64-bit pointer to represent Finalizable, Remapped, Marked1, and Marked0 (ZGC supports only 64-bit platforms) to mark the storage state that points to memory. It is equivalent to marking the information of the object on the pointer of the object. Note that the pointer here corresponds to a reference in Java terminology.

When the pointed memory changes (when the memory Compact is moved), the color changes.

As mentioned in G1, STW is required in the Compact phase, otherwise it will affect the execution of user threads. So how to solve this problem?

Due to the existence of the shading pointer, Load Barrier can easily know the storage state of the object in memory (accessing the object through the pointer) when the program is running, and the memory requested to read is shaded. Then the read barrier will be triggered. The read barrier updates the pointer and returns the result, which is costly to achieve the effect of concurrency with the user thread.

Understand these two technologies together and quote RednaxelaFX.

Compared with the traditional algorithms for marking objects, ZGC marks the pointer and adds a Load Barrier (read barrier) when accessing the pointer. For example, when the object is being moved by GC, the color on the pointer will be wrong, and this barrier will update the pointer to a valid address before returning, that is, only a single object will be read with a probability of deceleration, and there is no rough overall Stop The World in order to keep the application consistent with GC.

Although ZGC is still in the experimental stage of JDK 11, because the algorithm and idea is a great improvement, it is believed that it will become the mainstream GC collector in the future.

At this point, the study of "what are the JVM garbage collectors" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report