In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is to share with you about the common garbage collectors, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
As a Java developer, the garbage collector is a question that is often asked during the interview. With the development of Java, the garbage collector has experienced a lot of development. There are several kinds of garbage collectors that we are familiar with.
Garbage Collector of Serial single-threaded New Generation replication algorithm
SerialOld garbage collector is a single-thread old-age marking algorithm.
ParNew garbage collector is a multithreaded implementation of Serial, which is implemented by replication algorithm.
Parallel Scavenge garbage collector is an efficient multithreaded replication algorithm.
ParallelOld garbage collector is an old multithreaded tag finishing algorithm of Parallel Scavenge.
CMS garbage collector, which is a multithreaded tag removal algorithm, will be described in more detail later
The G1 garbage collector is a high-throughput garbage collector.
Recovery algorithm
Before introducing the garbage collector, let's take a look at the algorithm behind the garbage collector. Each garbage collector is the implementation of a specific algorithm. Different garbage collectors just have different algorithms behind them. Let's briefly introduce the specific algorithm.
Mark clear
The tag removal algorithm is an algorithm that marks first and then clears. In the first scan, it marks all the memory that needs to be cleaned, marks all the memory that needs to be reclaimed, and clears it at once. This algorithm is simple but inefficient, and memory fragmentation is serious. Once memory is severely fragmented, memory will be wasted and larger objects cannot be allocated.
Replication algorithm
The implementation of the replication algorithm is relatively simple and clear, that is, the memory is divided into two parts, only one of which is fixed in normal use, and when GC is needed, copy the surviving objects to another part, and then clean up all the memory that has been used. This algorithm can solve the problem of fragmentation, but the disadvantage is also obvious, is a waste of memory, half of the memory can not be used.
Tag finishing algorithm
Since tag removal and replication algorithms have their own advantages and disadvantages, it is natural for us to think about whether the two algorithms can be combined, so there is a tag finishing algorithm. The marking phase is the same as the mark clearing algorithm, which first marks the parts that need to be recycled, but the cleanup phase does not clean up directly, but moves the surviving objects to one end of memory, and then clears the rest.
Although the tag finishing algorithm can solve some of the problems of the above two algorithms, it still needs to be marked first and then moved, and the overall efficiency is still on the low side.
Generation recovery algorithm
Generation recovery algorithm is a widely used algorithm at present, this is not a new algorithm, but only the division of memory, different regions of memory use different algorithms. According to the survival time of the object, the memory is divided into the Cenozoic era and the old age, in which the Cenozoic era includes the Eden region and S0Magi S1. In the new generation, the replication algorithm is used. Only Eden and S0 regions are used for object memory allocation. When GC occurs, the surviving objects are copied to S1 area, and then replicated repeatedly. When an object is still alive after 15 times of GC, the object will enter the old age. In the old days, because there were fewer objects to be recycled each time, the tagging algorithm was used.
Garbage collector
Although several garbage collectors have been mentioned above, only CMS and G1 are the mainstream garbage collectors at present. Next, let's talk about these two garbage collectors.
CMS garbage collector
The full name of CMS is Concurrent Mark Sweep concurrent tag clearing garbage collector. CMS is a garbage collector designed to obtain the shortest pause time. When it comes to pause time, we all know that any garbage collector will have STW,Stop the World to stop the user process when it is working, which is only difficult for the business to accept, but now all garbage collectors on the market can not avoid this problem and can only maximize the optimization to reduce the pause time.
Although CMS is called a concurrent garbage collector, it is not completely concurrent. From the name, we can see that it is implemented by using the tag-cleanup algorithm. The whole implementation process is divided into five steps:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Initial markup: pause all threads, always reachability analysis to mark objects, which is also the first STW of the CMS garbage collector
Concurrent markup: when concurrent markup occurs, both the GC thread and the user thread exist at the same time, and all reachable objects are recorded during this process, but after this process, new reference updates are generated because the user thread is running all the time, that is, the next step is needed.
Concurrent pre-cleaning: at this stage, the user thread and the GC thread run at the same time, and the GC thread performs a pre-clean action.
Relabeling: this phase pauses the user thread and fixes the updates caused by the user thread during the previous step of concurrent marking, which is longer than the initial marking time, but a little shorter than the concurrent marking time.
Concurrent cleanup: the last step of cleaning is performed after all the objects that need to be cleaned have been marked. When cleaning up, the user thread can continue to run, and the GC thread only cleans up the marked area.
G1 garbage collector
G1 full name Garbage-First is a server-oriented garbage collector, which achieves a predictable pause time model by dividing heap memory into multiple Region. In G1, the new generation and the old age are no longer physically isolated, but are divided into Region regions. It is this predictable time pause model that makes G1 a high-throughput garbage collector. G1 can make full use of CPU and shorten the time of STW in multi-core environment.
The whole implementation process of G1 garbage collector is divided into four steps:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Initial tag: marks the directly associated object of GC Roots through reachability analysis, which requires STW as well as CMS
Concurrent tag: the concurrent tag finds the living object through GC Roots. This is the stage in which the GC thread runs at the same time as the user thread, and this stage takes longer than the initial tag.
Final tag: the final tag, like the CMS retag, is also used to correct new reference updates caused by the user thread continuing to run during the concurrent tag process; similarly, STW is also required here.
Filter recycling: filter recycling. Here, the recovery cost of each Region is sorted, and the recovery plan is made according to the pause time expected by the user, which is the embodiment of the predictable pause time model, in which the GC thread runs at the same time as the user thread.
These are the common garbage collectors, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.