In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Why the intergenerational citation is GC root, I believe that many inexperienced people do not know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Someone in a JVM group asked why the intergenerational reference is gc root. Although this is a very simple problem, it actually involves the core idea of generational garbage collection algorithm.
The basic explanation of gc root
First of all, we need to understand what a GC root is.
Figure: gc root
The heap is the memory space managed by our garbage collection. As shown in the figure, there are two kinds of references, one is the reference of the object outside the heap to the object in the heap, which is marked red, and the other is the reference between the objects in the heap, which is marked as gray. Usually what we call gc root can be thought of as a red reference, such as an object in the stack reference heap. Why don't we think that references between objects in the heap are gc root? Because our objects will eventually be used externally, such as being accessed by stack references. Therefore, if a large number of objects in the heap refer to each other, but without any external references to the heap, then these objects are actually unreachable. This is the case with HotSpot, where all objects in the heap are ultimately used by the stack. Therefore, U and V can be regarded as unreachable objects.
Generational and intergenerational citation
After explaining the basic concepts of gc root, let's take a look at the generational theory. Basically, modern garbage collectors are generational garbage collectors, which are based on two generational theories:
Weak Generation hypothesis (weak generational hypothesis): most objects die at a young age
Strong generational hypothesis (strong generational hypothesis): the older the object, the harder it is to die.
This generational hypothesis leads to the idea of garbage collection: objects are allocated to different areas according to their "age", and only one area is recycled at a time. This is the basic idea of generational recycling. Because obviously, if most of the objects die overnight, then put them together, each recycling can recover a lot of space; the rest of the objects that are not easy to die, put together, then they can be recycled at a very low frequency. This takes into account the time cost of garbage collection and the space utilization of memory.
The general garbage collection algorithm can be divided into at least two ages, the young generation and the old age. However, the simple generation theory has a huge defect in garbage collection: in order to find the surviving object in the younger generation, it has to go through the whole old age, and vice versa.
Figure: cross-generational citation causes the traversal of the old age
If we start traversing from the younger generation, then we can conclude that N, S, P, Q are all living objects. However, V will not be considered a living object, and the memory it occupies will be reclaimed. This is a huge loophole! Because U itself is an old object, and there are external references to it, that is to say, U is a living object, and U points to V, which means V should also be a living object! And this is all because we only go through the years to replace the object!
Therefore, in order to solve this problem of intergenerational references, the stupidest way is to traverse the objects of the old era and find out these intergenerational references. There is a great waste of performance in this scheme. Because from the two generational hypotheses, there is actually a corollary: intergenerational citations are rare. In order to find out a little bit of intergenerational citation, we have to go through the whole old age! From the figure above, it is obvious that we do not have to traverse R.
Therefore, in order to avoid the performance overhead of traversing the old age, the usual generational garbage collector introduces a technology called memory set. Simply put, a memory set is a table used to record intergenerational references.
Figure: memory set record intergenerational reference
As shown in the picture, with a memory set, we don't have to traverse the old age, which is a huge performance improvement!
Final explanation
Now, let's imagine that to recycle the younger generation, we first start by referencing the external references of the younger generation objects, and secondly, we start with intergenerational references. So we can naturally get the result: intergenerational citation is also gc root.
The whole model can be abstracted as follows:
Figure: final explanation of gc root
Appendix
After the introduction of the memory set, there will be a very interesting problem: even if the old object is in fact unreachable, because of the existence of the memory set, the intergenerational reference from the object will still be regarded as gc root until the object is recycled and the related items in the memory set are erased.
Figure: the problem caused by the memory set
As shown in the figure, U no longer exists an external reference, so it is virtually unreachable. But at this moment, because GC did not happen in the old days, it is still alive.
If we use the method of traversing the old age to find intergenerational citations, then we can only find S-> P this one. As a result, both U and V are treated as unreachable objects and their memory space can be reclaimed.
If we use a memory set, then the entry U-> V in the memory set still exists because U is not dropped by GC, so V will be treated as a living object when the younger generation collects it.
This problem is due to the "lag" brought about by the use of memory sets, which improves time efficiency, but reduces space utilization. In any case, it still ensures the principles followed by garbage collection: garbage collection ensures that collected objects must be unreachable objects, but does not ensure that all unreachable objects will be recycled.
After reading the above, do you understand why intergenerational citation is the method of GC root? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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