In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about how GC enumerates root nodes quickly. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
One of the advantages of Java is GC (Garbage Collection). Although it can help us manage memory, it will STW (Stop the World) when it works. That is to stop all working threads, "Don't work yet, I'll clean up the garbage first!" .
Then there is a problem, ah, think about it, for example, when you are playing a game, the computer will STW and stop for a few seconds to clean up the garbage. You may be "*", and your teammates may also "* *".
But now some commonly used garbage collectors have to STW, although a variety of garbage collectors have racked their brains to reduce STW time, but can not avoid STW. Specific comparison of various garbage collectors write a separate article next time.
Today, let's talk about how GC enumerates root nodes quickly. In the HotSpot virtual machine, reachability analysis is used to determine whether this object needs to be reclaimed. Then accessibility analysis needs to find the "source", the root node.
By enumerating a root node (GC Roots), and then feeling it all the way down, and then those objects that are not touched are clicked and recycled. Then the process of following the trail must stop the world, that is, those working threads have to stop.
If you think about it, if the object reference relationship changes without STW, how will the garbage collector snap the object? it's easy to click wrong, then we users will be anxious. So the STW is inevitable when enumerating the root node, so you can only keep the STW as short as possible.
The root node is mainly in the global reference (constant, class static property) and the execution context (the local variable table in the stack frame). Then if we want to find it one by one, it will be very slow.
And our HotSpot is the accuracy GC, that is, it needs to know the type of data at a certain location, and the type is accurate. In this way, it can accurately know whether this data type is the pointer it cares about, that is, the reference!
In HotSpot, a structure called OopMap is used to store what type of data is offset in an object. It is recorded during class loading.
OopMap can be understood as an additional message, or the tag of a piece of clothing. We can see what the dress is made of by looking at the tag. So GC can directly look at these "hanging tags" to get the information when scanning.
When JIT compiles, the OopMap will also be recorded in some specific locations, recording which positions on the stack and in the register are referenced when executing an instruction to the method. Each method may have many OopMap, which is determined according to the specific location. This particular location will divide the method into several pieces, each with an OopMap.
These specific locations are mainly:
1. Before the method returns / after calling the method's call instruction
2. The end of the loop
3. Where an exception may be thrown
These specific locations are also called Safepoint.
The reason for recording OopMap in a specific location is that if you record each instruction, it will require a lot of space and increase the space cost of GC, so using some key points to record can effectively reduce the space needed for recording.
So GC doesn't come anytime and anywhere, and you can't start GC until you reach a safe point.
Usually OopMap is compressed in memory, and it will be decompressed only when GC is needed, and then the corresponding offset will be scanned all the time.
For the JNI (Java Native Interface) method, there is no OopMap because the native method has nothing to do with the interpreter or JIT compiler.
Its reference is the same as adding a middle layer, that is, the handle, that is, the reference does not point directly to the object in the heap, but the reference points to the handle and the handle points to the object in the heap. So GC scans the handle directly, and there is no need to scan stack frames.
This is how the GC shared by the editor enumerates the root nodes quickly. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, 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.