In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how the GC principle of JVM memory layout in Java, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
Overall structure diagram
Main process of GC
At first, the objects were allocated in the eden area, S0 (that is, from) and S1 (that is, to), almost empty.
With the running of the application, more and more objects are assigned to the eden area.
When the Eden area can not fit, minor GC will occur (also known as young GC). The first step is of course to identify the unreachable garbage object (i.e., the yellow block in the following image), then move the reachable object to S0 area (i.e., 4 light blue squares to S0 area), and then clean up the yellow garbage block. after this round, the eden area becomes empty.
Note: in fact, the "[tag-clean eden] + [tag-copy eden- > S0] algorithm has been used here.
Continue, with the continuous allocation of objects, the eden may be empty again, and the minor GC process will be repeated, but it should be noted that S0 is empty at this time, so the roles of S0 and S1 will actually switch, that is, the surviving objects will move from eden and S1 to S0. Then remove the garbage from eden and S1, and after this round, eden and S1 become empty, as shown in the following figure.
* * for those who compare "longevity", they have been moving back and forth between S0 and S1, which not only takes up a lot of space, but also causes some expenses and reduces the efficiency of gc, so there is "age" and "promotion". * * subjects move from one edge,s0,s1 to another in the Cenozoic era, with an age of + 1. When the young area reaches a certain age threshold, it will be promoted to the old age. The figure below is 8, that is, if you are still alive after moving 8 times, the next time you minor GC, you will move to the Tenured area.
The following is the main process of promotion: the object is first assigned to the new generation, and after many times of Young GC, if the object is still alive, it is promoted to the old age.
If the old age is finally full, major GC (that is, Full GC) will occur, because there are usually more objects in the old era, because marking-cleaning-finishing (compression) usually takes a long time, which will make applications stutter, which is why many applications need to be optimized to avoid or reduce Full GC (STW) as much as possible.
Important summary (you are welcome to take it away):
If the new object assigned is relatively large, the Eden area cannot be put down after the YGC, but when the old area can be put down, it will be directly assigned to the old area (that is, there is no promotion process, directly to the old age).
If the new object allocated is relatively large, the Eden area can not be put down, and the YGC finds that the S (TO) area can not be put down, but when the old area can be put down, it will be directly promoted to the old area.
If the new object allocated is relatively large, the Eden area cannot be put down, and the YGC later finds that the S (TO) area can fit, but if it exceeds the pre tenured threshold value, it will be promoted directly to the old area.
GC recycler
Not counting the latest artifact ZGC, there have been seven classic garbage collectors in history.
These collectors are all based on generation, with the exception of G1, according to the generation of recovery, three kinds of collectors above the horizontal line: Serial, ParNew, Parellel Scavenge are recycled Cenozoic, and three below the horizontal line: CMS,Serial Old, Parallel Old are recycled in the old age.
New Generation Serial Collector for GC Collector
Single thread with mark-copy algorithm, quick knife to cut the mess, single-threaded benefits to avoid context switching, early machines, mostly single-core, but also more practical. However, during execution, STW (Stop The World) occurs.
ParNew collector
The multithreaded version of Serial, which will also be STW, is more suitable for multi-core machines.
Parallel Scavenge collector
The upgrade version of ParNew (focus on throughput), the main difference is that it provides two parameters:-XX:MaxGCPauseMillis maximum garbage collection pause time (time is controlled by sacrificing the collection range of the Eden area) -XX:GCTimeRatio ratio of garbage collection time to total time (mainly by predicting the relevant execution time by calculating the average collection time in advance). Through these two parameters, you can properly control the rhythm of collection and pay more attention to throughput, that is, the ratio of total time to garbage collection time. Make full use of the execution time of CPU to improve the throughput efficiency. But the recovery time should not be the fastest and shortest.
The Old Age of GC Collector Serial Old Collector
Because there are usually more objects in the old era, and they usually take up more space, if you use the replication algorithm, you have to set aside 50% of the space for replication, which is quite uneconomical, and because there are so many objects, it takes a long time to copy from one area to another, so the old-era collection usually uses the "mark-organize" method. As you can see from the name, this is single-threaded (serial), and there will still be STW.
Parallel Old collector
Bottom line: the multithreaded version of Serial Old.
CMS collector
Full name: Concurrent Mark Sweep, from the name, you can guess that it is concurrent multithreading. This is a widely used collector in JDK 7. It is necessary to say more and borrow a picture from a netizen:
This is obviously much more complex than the Serial Old collector or Parallel Old collector, which is divided into four phases:
1) Inital Mark initial tag: mainly marks the subordinate (note: only one level) object at the beginning of GC Root, this process will STW, but there will not be many subordinate objects directly associated with GC Root, so this process is actually very fast.
2) Concurrent Mark concurrent tagging: based on the results of the previous step, continue to identify all associated objects down to the end of the chain. This process is multithreaded, and although it is theoretically time-consuming, other worker threads do not block and there is no STW.
3) Remark sign again: why mark it again? Because step 2 does not block other worker threads, other threads are likely to generate new garbage during the identification process.
4) Concurrent Sweep: parallel cleanup. Here, multithreading is used to clean up the garbage with "Mark Sweep- tag" algorithm. Other worker threads can still continue to branch without stutter.
Just imagine, the garbage cleaner on the high-speed railway began to shout from one end of the carriage, "passengers who need to throw away the garbage, please throw away the garbage", walking forward while working, and so on when they get to the other end of the carriage, where they have just passed, some passengers may have produced new empty bottles of rubbish. So, to clean up the car completely, she should shout: all passengers stop throwing rubbish (STW), and then collect the newly generated garbage.
Of course, because the garbage has been collected just now, it will not take long to collect the newly generated garbage this time (that is, the STW time will not be very long).
Wait, didn't we just mention the "tag cleanup" method, which leaves a lot of memory fragments? Indeed, but there is no way, if replaced with the "Mark Compact marking-sorting" method, after garbage cleaning, the remaining objects will also be sorted out, which will lead to changes in the memory addresses of these objects. Don't forget that other threads are still working at this time, and if the referenced object addresses change, the world will be in chaos.
Although it is still not perfect, from the four-step processing process, the most criticized long-term STW in the previous collector is decomposed into two short-lived STW through the above design, so in general, the stutter situation during the application in GC will be greatly improved, which is also an important reason why CMS was once very popular.
CMS collector drawbacks floating garbage
Since the user thread is still running during the CMS concurrency cleanup phase, new garbage will naturally be generated with the program running, and after the marking process, CMS cannot dispose of them in the current collection, so it has to wait until the next time GC cleans up. This part of the garbage is called floating garbage.
Under the default setting of jdk1.5, the CMS collector will be activated when 68% of the space is used in the old age, and the trigger percentage can be increased by the value of-XX:CMSInitialOccupancyFraction. In jdk1.6, the CMS startup threshold is raised to 92%. If the memory reserved during the CMS run does not meet the needs of the program, there will be "Concurrent Mode Failure", and then downgrade temporarily enable the Serial Old collector for garbage collection in the old years. There will be a long pause. (when there is concurrent cleanup, business threads can still be generated. As a result, when CMS is reclaimed, the memory of new objects cannot be stored too fast, resulting in CMF)
So too high a XX:CMSInitialOccupancyFraction setting can easily lead to a large number of "Concurrent Mode Failure". There's space debris.
CMS is based on the "mark-clear" algorithm, so it will produce space debris. To solve this problem, CMS provides the-XX:UseCMSCompactAtFullCollection development parameter to turn on memory defragmentation, which can lead to longer pause times because memory defragmentation cannot be parallel. There is also-XX:CMSFullGCBeforeCompaction, which is used to set how many times the FullGC is not compressed, followed by one with compression (default is 0).
Sensitive to CPU resources
The default number of recycled threads started by CMS is (number of cpu + 3) / 4. Therefore, a small number of CPU will lead to a greater reduction in the execution speed of user programs. This is one of the reasons why it reduces throughput.
G1 collector
The full name of G1 is Garbage-First. In view of some shortcomings of CMS, such as memory fragmentation in the old days, STW time has improved a lot, but there is still room for improvement. G1 came out of nowhere, it for the memory of the heap area is very novel, a bit of the algorithm in the divide and conquer method "divide and conquer" flavor.
As shown in the figure below, G1 divides the heap memory area into Region regions of equal size (1-32m power n) and continuous memory. Each region corresponds to one of the four roles of Eden, Survivor, Old and Humongous, but continuity is not required between region and region.
Note: Humongous, referred to as H Zone, is an area dedicated to storing very large objects, usually > = 1 Region Size 2 Region Size, and H Zone will be recycled only in the Full GC phase, avoiding frequent scanning, copying / moving large objects.
All garbage collection is based on a single region. JVM knows which region has the fewest objects (that is, the area is the most empty) and will always give priority to collecting these region (because there are few objects, memory is relatively empty, so it must be fast), which is the origin of the name Garbage-First, G is the abbreviation of Garbage, 1 is First.
Before G1 Young GCyoung GC:
After young GC:
In theory, garbage collection can be done as long as there is an Empty Region (empty area).
Since continuity is not required between region and region, and scenarios that use G1 usually have large memory, such as 64G or more, to improve the efficiency of scanning root objects and tags, G1 uses two new secondary storage structures:
Remembered Sets: referred to as RSets, each Region has its own RSets, depending on where the objects in each region come from (that is, who quoted me). (Other Region-> Self Region).
Collection Sets: CSets for short, records the collection of Region waiting to be reclaimed, and the objects in these Region are copied or moved when GC.
With the introduction of RSets, taking the RSets of the new generation of Region as the root object in YGC can avoid scanning the old region and greatly reduce the burden of GC. Note: when collecting Mixed GC in the old days, RSets recorded references to Old- > Old, which also avoided scanning all Old areas.
Mixed GC is also known as Old Generation Collection
According to the description of the oracle official website, it can be divided into five stages: Initial Mark (STW)-> Root Region Scan-> Cocurrent Marking-> Remark (STW)-> Copying/Cleanup (STW & & Concurrent)
Note: there are also many articles that omit Root Region Scan and merge it into Initial Mark into four stages.
The "initial tag" of the surviving object depends on the fact that the Young GC,GC log will record the word young.
2019-06-09T15:24:37.086+0800: 500993.392: [GC pause (G1 Evacuation Pause) (young), 0.0493588 secs] [Parallel Time: 41.9 ms, GC Workers: 8] [GC Worker Start (ms): Min: 500993393.7, Avg: 500993393.7, Max: 500993393.7, Diff: 500993393.7] [Ext Root Scanning (ms): Min: 1.5, Avg: 2.2, Max: 4.4, Diff: 500993393.7] Sum: 17.2] [Update RS (ms): Min: 15.8, Avg: 18.1, Max: 18.9, Diff: 3.1, Sum: 144.8] [Processed Buffers: Min: 110, Avg: 144.9, Max: 163,Diff: 53, Sum: 1159] [Scan RS (ms): Min: 4.7, Avg: 5.0,5.1Max: 5.1, Diff: 1159] Sum: 39.7] [Code Root Scanning (ms): Min: 0, Avg: 0, Max: 0, Diff: 0, Sum: 0] [Object Copy (ms): Min: 16.4, Avg: 16.5, Max: 16.6, Diff: 0.2, Sum: 132.0] [Termination (ms): Min: 0, Avg: 0, Max: 0, Diff: 0 Sum: 0.0] [Termination Attempts: Min: 1, Avg: 4.9, Max: 7, Diff: 6, Sum: 39] [GC Worker Other (ms): Min: 0.0, Avg: 0.0, Max: 0.1, Diff: 0.1, Sum: 0.3] [GC Worker Total (ms): Min: 41.7, Avg: 41.8, Max: 41.8, Diff: 0.1] Sum: 334.1] [GC Worker End (ms): Min: 500993435.5, Avg: 500993435.5, Max: 500993435.5 Diff: 0.0] [Code Root Fixup: 0.0 ms] [Code Root Purge: 0.0 ms] [Clear CT: 0.2 ms] [Other: 7.2 ms] [Choose CSet: 0.0 ms] [Ref Proc: 4.3 ms] [Ref Enq: 0.1 ms] [Redirty Cards: 0.1 ms] [Humongous Register: 0.1 ms] [Humongous Reclaim: 0. 1 ms] [Free CSet: 0.6 ms] [Eden: 1340.0M (1340.0M)-> 0.0B (548.0M) Survivors: 40.0m-> 64.0M Heap: 2868.2m (12.0G)-> 1499.8m (12.0G)] [Times: user=0.35 sys=0.00 Real=0.05 secs]
During the concurrent marking process, if some region is found to be completely empty, it will be cleared directly.
Enter the relabeling phase.
Concurrent replication / inventory phase. At this stage, objects in the Young area and the Old area may be cleaned at the same time. The GC log is recorded as the mixed field, which is why G1 is collected in its old age, also known as Mixed GC.
2019-06-09T15:24:23.959+0800: 500980.265: [GC pause (G1 Evacuation Pause) (mixed), 0.0885388 secs] [Parallel Time: 74.2 ms, GC Workers: 8] [GC Worker Start (ms): Min: 500980270.6, Avg: 500980270.6, Max: 500980270.6, Diff: 500980270.6] [Ext Root Scanning (ms): Min: 1.7,2.2, Max: 4.1, Diff: 500980270.6] Sum: 17.3] [Update RS (ms): Min: 11.7, Avg: 13.7, Max: 14.3, Diff: 2.6, Sum: 109.8] [Processed Buffers: Min: 136, Avg: 141.5, Max: 152, Diff: 16, Sum: 1132] [Scan RS (ms): Min: 42.5, Avg: 42.9, Max: 43.1, Diff: 109.8] Sum: 343.1] [Code Root Scanning (ms): Min: 0, Avg: 0, Max: 0, Diff: 0, Sum: 0] [Object Copy (ms): Min: 14.9, Avg: 15.2, Max: 15.4, Diff: 0.5, Sum: 121.7] [Termination (ms): Min: 0, Avg: 0, Max: 0, Diff: 0 Sum: 0.1] [Termination Attempts: Min: 1, Avg: 8.2, Max: 11, Diff: 10, Sum: 66] [GC Worker Other (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.2] [GC Worker Total (ms): Min: 74.0, Avg: 74.0, Max: 74.1, Diff: 0.1 Sum: 592.3] [GC Worker End (ms): Min: 500980344.6, Avg: 500980344.6, Max: 500980344.6 Diff: 0.0] [Code Root Fixup: 0.0 ms] [Code Root Purge: 0.0 ms] [Clear CT: 0.5 ms] [Other: 13.9 ms] [Choose CSet: 4.1 ms] [Ref Proc: 1.8 ms] [Ref Enq: 0.1 ms] [Redirty Cards: 0.2 ms] [Humongous Register: 0.1 ms] [Humongous Reclaim: 0. 1 ms] [Free CSet: 5.6 ms] [Eden: 584.0M (584.0M)-> 0.0B (576.0M) Survivors: 28.0M-> 36.0m Heap: 4749.3M (12.0G)-> 2930.0M (12.0G)] [Times: user=0.61 sys=0.00 Real=0.09 secs]
Through the analysis of these stages, although it seems that STW will still occur in many stages, G1 provides a prediction model to predict the collection through statistical methods, based on historical data, how many Region needs to be selected for recovery, as far as possible to meet the expected pause value of users (the-XX:MaxGCPauseMillis parameter can specify the expected pause value).
Note: if Mixed GC still doesn't work well enough to keep up with the need for new objects to allocate memory, Serial Old GC (Full GC) will be used to force the collection of the entire Heap.
Summary: compared with CMS, G1 has a memory demarcation process (mark-compression) to avoid memory fragmentation; STW time is controllable (can predict GC pause time).
The above is what the GC principle of JVM memory layout in Java is. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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.