In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how to tune the G1 garbage collector in the concurrent scene". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to tune the G1 garbage collector in the concurrent scene" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.
G1 Overview 1, maximum heap size
The maximum heap size managed by G1 is 64G. The size of each Region is set by-XX:G1HeapRegionSize, and the size is 1~32MB. By default, up to 2048 Region,G1 can manage the maximum heap memory is 32MB*2048=64G.
The minimum heap memory for using the G1 garbage collector should be 1MB*2048=2GB. Below this value, it is recommended to use a different garbage collector.
2. Region size
The size of Region is 1~32MB, and the specific values include 1MB, 2MB, 4MB, 8MB, 16MB, 32MB region size optimization is related to large objects. When objects occupy more than half of the memory of Region, they will be regarded as large objects.
Being marked as a large object is not good for garbage collection.
3. Get the default value
Check the current default values of the local JVM, especially the G1 garbage collector.
Java-XX:+PrintFlagsInitial > > ~ / 1.txt three GC modes
The G1 garbage collector has two garbage collection modes, new generation collection and mixed collection, and will switch to Full GC in special cases.
1. Cenozoic recycling
The new generation of recycling will dispose of all the garbage in the Eden area during the maximum pause time. The specific operation is to copy all the living objects in the Eden area to the Survivor area, while emptying the Eden area.
The new generation recycling is accompanied by the application pause, and the longest pause time does not exceed the maximum pause time. Although the new generation recycling has a pause mechanism, considering the characteristics of parallel recycling, the recovery logic is relatively simple and the recovery efficiency is still high. Generally speaking, the actual recovery time of the new generation is usually lower than the maximum pause time.
The new generation of recycling is triggered when the newly created object cannot find enough storage space in the Eden area.
2. Mixed recovery
Mixed recycling is accompanied by new generation recycling and old-age recycling, which will dispose of most of the garbage in the Eden area and part of the old-age garbage within the maximum pause time.
There is no doubt that recycling in the old days will be accompanied by a suspension of applications. The mixed recovery operation is more complex, compared with the new generation recovery, the number of garbage recovered per unit time is less, and the recovery efficiency is low. In general, the actual time spent on mixed recycling is usually close to or equal to the maximum pause time.
The trigger time of mixed recycling is controlled by the parameter InitiatingHeapOccupancyPercent. The default value is 45, which means that if the ratio of the space occupied in the old age to the total size of the heap exceeds this number, mixed recycling will be triggered.
The default value of 45% is reasonable and the so-called tuning is not recommended. The old recycling strategy is also to copy the living objects in the selected Region area to the free Region area. Mixed recycling along with the recycling of the new generation of garbage can clear out a larger free Region area to store the surviving objects in the elderly area, ensuring that the recycling process can be carried out normally.
There are generally more surviving objects in the elderly area, and it takes a long time for objects to be copied in memory, so the efficiency of mixed recovery is relatively low.
3 、 Full GC
Full GC is a situation that all G1 garbage collection tuners try to avoid, single-thread garbage collection, the collection object is the entire heap, no longer subject to the maximum pause time constraints, once this occurs, it means that the response time of the application is mercilessly longer.
When the application enters the Full GC state from time to time, instead of allowing its single thread to reshape the heap memory, it is better to adopt the redundancy strategy to restart the application one by one and actively reshape the heap memory space at the time of low traffic.
The phenomenon of Full GC during the peak period of traffic and its coping strategies will be discussed later.
Default parameter 1, heap memory parameter default value indicates optimization recommendation MaxGCPauseMillis200ms maximum pause time G1HeapRegionSize is not set heuristic inference G1NewSizePercent5 new generation minimum percentage G1MaxNewSizePercent60 new generation maximum percentage 2, new generation memory recovery parameter default value indicates that optimization recommended ParallelGCThreads parallel GC threads, default value MaxTenuringThreshold15 will be inferred from the number of CPU cores to the old age threshold SurvivorRatio8Eden and a proportional TargetSurvivorRatio50Survivor area memory utilization rate of a Survivor Increasing this value will reduce the probability of old age + whether G1EagerReclaimHumongousObjectstrue collects large objects during YGC. 3. The default value of mixed recycling parameters indicates that the larger the G1MixedGCCountTarget8 value of the optimization recommendation is, the fewer partitions are collected. G1OldCSetRegionThresholdPercent10 means that up to 10% of the partition garbage collected at one time is transferred in the heap.
The key to tuning the garbage collector is to minimize the frequency of Mixed GC, in other words, to minimize the flow of garbage to the old days. GC tuning is to understand the flow law of garbage in the heap, so as to interfere with the garbage flowing to the old era in advance, so as to keep it in the new generation as much as possible.
In the new generation of garbage (mainly refers to the Eden area), garbage collection uses YGC, the collection thread is carried out concurrently with the application thread, and garbage collection is transparent to the application. If the computing power of CPU is sufficient, the application can hardly feel that the garbage is being collected.
Garbage in the old days, garbage collection uses Mixed GC, when the collection thread starts to work, the application thread blocks, waits for the collection thread to finish its work, and the application thread is awakened again. Frequent Mixed GC has a negative impact on the throughput of applications.
1. How does the object enter the old age
In general, newly created objects will exist in the new generation of Eden area, and the next garbage collection penalty will be collected directly. If the object is tenacious (continues to be referenced by other objects), it will be transferred in the Survivor area, and every GC will still not be garbage collected, then the age plus one will continue to flow in S0 and S1, and when the age reaches a certain threshold, it will go straight into the old age.
(1) the big object goes directly to the old age.
If the newly created object is too large, it will enter the old age without going through the new generation. The size threshold of the control object is determined by the parameter-XX:PretenureSizeThreshold, in bytes.
(2) dynamic age judgment.
In addition to the repeated age changes of objects in S0 and S1 areas, garbage collection maintains another set of independent age determination rules: if the garbage that has not been recycled after YGC exceeds 50% of the Survivor area, then these objects will directly enter the old age.
12g * 60% * 10% * 50% * 1024 = 737MB
The dynamic age determination rule requires that each YGC should be as thorough as possible, which means that the maximum time of each GC should not be too short. The default value of 200ms is reasonable.
If the preset maximum pause time is too short, then there is a large amount of garbage that has not been recycled after each GC, the capacity of S area is limited, garbage that should not enter the old age accumulates quickly in the old age, frequent Mixed GC is inevitable.
2. High concurrency accelerates into the old era
In high concurrency scenarios, CPU and memory resources are tight, the load is high, and uncertain performance jitter accelerates garbage into the old era.
For example, the DAO layer queries the database, and at the end of a complete session, all the object garbage generated in the entire session should be collected in the Eden area. Due to network fluctuations and the limitation of database processing capacity, a large number of sessions timed out. In this process, this part of the object garbage is likely to superimpose age in the rapid S0 and S1 flow, or trigger dynamic age determination, directly into the old age.
In the old days, there was not enough memory space, and the direct side effect of triggering Mixed GC,Mixed GC was the application stutter.
Tuning step 1, setting up the garbage collector
Java 8 needs to specify the G1 garbage collector manually, and add the-XX:+UseG1GC parameter on the command line.
2. Set the heap size
There are two points to note in setting the memory heap size: the initial heap size is the same as the maximum heap size, and the heap size accounts for 75% / 80% of the physical memory size, which reserves the necessary memory for the system core services.
Parameter-Xmx12G sets the initial heap size; parameter-Xms12G sets the maximum heap size.
3. Meta-space setting
Metaspace refers to the memory area that stores special variables such as static classes, static methods, constants, and so on.
Parameter-XX:MetaspaceSize=1G sets the initial size of metaspace; parameter-XX:MaxMetaspaceSize=1G sets the maximum size of metaspace.
4. GC pause time
GC pause time refers to the maximum time of each YGC or Mixed GC. The garbage collector will dynamically select the range of garbage scanning according to the expected time set by the user. If the setting time is too small, some garbage may not be collected. In milliseconds.
-XX:MaxGCPauseMillis=2005, Cenozoic size
Parameter-XX:G1NewSizePercent sets the initial size of Cenozoic, default is 5%, and parameter-XX:G1MaxNewSizePercent sets the maximum size of Cenozoic, default is 60%.
The inner part of the Cenozoic era is subdivided into Eden zone and two Survivor. The default ratio is 8:1:1.
Eden: 12G * 60% * 80% = 5.76GS0: 12G * 60% * 10% = 0.72GS1: 12G * 60% * 10% = 0.72g
Assuming that the concurrent system creates 500MB objects every second, and assuming that each YGC can scan the Eden Region according to the maximum pause time set in advance, then the concurrent system needs to YGC about every 10 seconds.
Tuning practice
GC garbage collection tuning is carried out under the limitation of physical hardware and the theoretical space of tuning. If conditions permit, upgrading the hardware configuration, especially the physical memory configuration, can effectively reduce the GC frequency. Such as 8C32G or 16C64G.
1. Frequent YGC
When the concurrency is large, frequent YGC is inevitable, the unit time class creates more objects and becomes garbage after use. Frequent YGC has the possibility to accelerate the flow of S-zone objects to the old age, and try to ensure that the actual time spent on each YGC is lower than the default maximum garbage collection time (default 200ms), so that the new generation of garbage can be cleaned up every time, delaying the flow of garbage to the old age as much as possible.
2. Frequent Mixed GC
In the G1 garbage collector, there is no so-called concept of Mixed GC. Mixed GC is similar to F. GC, except that Mixed GC not only recycles the old age, but also recycles the new generation. What they have in common is that they all produce STW.
The essence of frequent Mixed GC is that a large number of garbage that should be recycled in the new generation has entered the old era, and the solution is to find out which garbage (objects) should be left in the new generation, but transferred to the old age.
(1) large objects
Check whether the application periodically creates large objects, and the threshold of large objects is controlled by the parameter-XX:PretenureSizeThreshold. If you increase this value appropriately on the premise that there is optimization space in the memory, it must not exceed half of the S area (there do not seem to be such large objects). The side effect is that the number of objects stored in the new generation becomes smaller, the memory in the Eden area is used up faster, and the YGC becomes more frequent accordingly.
From a business point of view, large objects must have their own reasons. From this point of view, the possibility of optimization is not high. Garbage collector optimization shields the business layer code as much as possible. After all, it is unrealistic to ask developers not to create large objects.
(2) Metaspace
Meta-space exhaustion will also lead to Mixed GC. Considering the particularity of meta-space storage content, there is no good way to increase GC frequency due to meta-space depletion. Simply increasing the meta-space size will compress the size of the new generation, the new generation will become smaller, the number of objects transferred to the old era will become more, and the memory consumption in the old era will be accelerated, which will also increase the frequency of GC.
Relatively speaking, increasing physical memory is a better solution to Mixed GC caused by running out of meta-space.
3 、 Full GC
Although the application temporarily stops responding when the Mixed GC is triggered (the default is 200ms), the pause time is relatively controllable.
If the idle Region cannot save the surviving objects during Mixed GC, and when the Mixed GC fails to work properly, garbage collection will switch to the Serial Old GC outside G1 to collect the entire heap, including Cenozoic, old age, metaspace, and so on.
When you enter the state of Serial Old GC garbage collection, garbage collection is no longer restricted by the maximum collection time, and a single thread is used for marking, cleaning and compression, and the application may enter a state of suspended animation. Maybe it would be better to restart the application, reallocate the heap memory, and shuffle the heap memory thoroughly.
The key to G1 garbage collection tuning is not to appear Full GC, so do not arbitrarily tune sensitive parameters, otherwise not only can not achieve the ideal, but even worse.
After reading this, the article "how to tune the G1 garbage collector in concurrent scenarios" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to 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.