In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
The new generation is divided into Eden area and Survivor area, Survivor is composed of From area and To area, complete memory structure, I will draw for you, don't draw, pass me the pen, I will draw, as shown in the following figure.
Interviewer: Oh, the picture is fine, so why is the heap divided into the new generation and the old age?
Me: to manage memory more effectively, of course.
Interviewer: what do you say?
Me: suppose that if there is a whole block of memory regardless of the old and new generations, the garbage collector will collect those long-standing objects together with those with a very short life cycle every time. Generally, objects with a long life cycle may be consistent with the application life cycle, and you can basically not recycle them, such as Bean management related objects (ApplicationContext) in the Spring framework, which exist throughout the running of the application. This kind of recycling is usually put in the old era after several times of recycling, but if you do not distinguish between the new and old generations, it will consume a lot of performance every time.
After distinguishing between the new generation and the old generation, the old generation releases the long-term survival objects, the new generation releases the objects with short life cycle, the old age objects are very stable, the new generation recycling does not affect the old era, and the recovery efficiency can be greatly improved.
Interviewer: then why does the new generation divide into Eden, From and To regions?
Me: [it's starting to get interesting]
First of all, most of the object life cycle is very short, if the new generation is not divided into multiple regions, the new generation may have two recycling schemes
The first possibility: each collection is carried out on a whole block of Cenozoic memory, and the complete garbage collection process is divided into three steps:
You need to find the object tags that need to be cleaned first.
Clean up these tagged objects
Move the remaining objects, and move the objects that reach the promotion age of the old age to the old age.
After the object is recycled, it will produce a lot of memory fragments (a lot of objects are recycled). If you want to solve the memory fragmentation, you need to move the remaining objects (tagging algorithm), and the whole recycling process is very inefficient.
The second possibility: if there is no Survivor area (From + To), the surviving objects are sent directly to the old age in the process of Minor GC (new generation recycling), so that the old age is quickly filled and Major GC is triggered (because Major GC is usually accompanied by Minor GC and can also be seen as triggering Full GC), Full GC frequently affects the execution and response speed of the program.
The new generation of recycling is called Minor GC, and the old generation of recycling is called Major GC.
Interviewer: why set up two Survivor zones? From and To
Me: let's take a look at the new generation of memory recycling process if there is only one Survivor area.
According to the picture above, the Eden area is full for the first time, and memory recovery is very simple. You can directly put the surviving objects in the Eden area into the Suvivor area.
For the second memory collection, you need to recycle two places, the Eden area and the Survivor area.
Because the objects that will survive in the Survivor area need to be reclaimed, the tag collation garbage collection algorithm should be used for the Survivor area (first mark the objects that need to be cleaned, then collect them, and then put the remaining surviving objects together)
The Eden region uses the replication algorithm to copy the objects stored in the Eden area to the Survivor area, and then clear the whole Eden area.
Seeing some articles on the Internet, it is said that the reason for setting up two Survivor areas here is to avoid memory fragmentation, because he assumes that in the second (and subsequent) recycling, memory collection is to recycle the Eden area first, and then the Survivor area, so of course there will be memory fragments, but if there is only one Survivor area, the garbage collection designer must first recycle the Survivor area, then the Eden area, and wait for the Survivor area to be recycled. Then move the Eden area to the Survivor area, so that the survival address is contiguous and there is no memory fragmentation. So the real reason is the efficiency problem I'm talking about below.
Interviewer: what's wrong with that?
Me: there are several problems with this:
What happens when the Survivor area is full after several recycles? Just move to the old days? The old age soon exploded. Move to the Eden area? The memory fragmentation is generated, and maybe after the Survivor area and Eden area are reclaimed, you still need to clean up the memory to remove the memory fragments, and the performance consumption is also very large.
In general, the performance consumption of the tag finishing algorithm is higher than that of the replication algorithm, especially in the new generation, 98% of the objects are "life and death", 98% of the objects are clearly marked, and only 2% of the objects are left. It is necessary to organize the memory, otherwise the 2% of the objects are directly put in another place and the whole block of memory is cleared. Eden the whole block of memory is very efficient.
So in the final analysis, the two Survivor regions are still for performance considerations, and the tag replication algorithm is more efficient than tag finishing.
Interviewer: then tell me more about the tag replication algorithm of the new generation of tags in addition to Eden and two Survivor regions.
Angela: 98% of the objects in the Cenozoic era are "born and die", so it is not necessary to divide the space between Eden and Survivor according to the ratio of 1: 1, but to divide the Cenozoic generation into a larger Eden space and two smaller Survivor spaces, using only Eden and one of the Survivor [0] (From area) at a time, leaving Survivor [1] (To area) for tag replication.
When recycling, copy the surviving objects in Eden and Survivor [0] to another piece of Survivor [1] (To) space at once, and finally clean up the Eden and the Survivor space you just used.
Another point: the From region and the To region will turn to each other after each Minor GC, the From region becomes the To region, and the To region becomes the From region, which is just a logical identification.
The HotSpot virtual machine defaults to 8: 1 of the size ratio of Eden to Survivor (CMS is not applicable), that is, the available memory space in each new generation is 90% of the entire new generation capacity (80% + 10%), and only 10% of the memory is "wasted" (10% of the memory (Survivor To area) is not stored all the time).
Tag replication algorithm flow:
Eden area + Survivor From area is full, mark the surviving object, mark it, and copy the surviving object to Survivor To area.
The Survivor To area becomes the From region (a logical identifier), and the From region becomes the To region.
Memory allocation, continue step 1, the replication process has reached the old age promotion age (default 15), moved to the old age.
This is the answer to the question about the new generation of unmarked removal algorithm in the foundation of JVM. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.