“This is the 17th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”

🔉 introduction

So far object decision and garbage collection algorithms, let’s talk about the details of HotSpot algorithm implementation. Algorithm algorithm, efficiency first, to ensure that the virtual machine is running efficiently.

Root node enumeration

Don’t be afraid, root node enumeration, for proper nouns all use the vernacular treatment, first talk about the root node, where we have seen the root node ah? Huh? Maybe you forgot, we mentioned the root node in the judgment of an object “portal”, is GCRoots, enumeration is traversal search, a scene that we mentioned before, accessibility analysis, if we want to find the object is garbage objects, we have to see whether it on GCRoots reference chain, but if you check each one, A waste of time, no doubt.

The root node enumeration step requires suspending all user threads, which is called Stop the Wrold. Why does this operation require suspending all user threads? Who influences the accuracy of the accessibility analysis? Let’s think about it: Let’s think about what would happen if we didn’t pause the user thread. It is possible that the references between objects will change frequently, so we cannot guarantee the accuracy of the reachability analysis because it may be garbage one moment and not the next, so we must ensure that the references are frozen while the root node enumeration is performed.

When the user thread stops, we do not need a complete check of all execution contexts and the location of all references. The virtual machine is so smart, it must be able to directly get our object reference, so we have to suspend 🔨 virtual machine (nefited 😈). There is a group called: The data structure of OopMap is stored. After the class loading action is complete, the virtual machine will calculate what type is at what offset and store it so that the collector can get the reference information directly when scanning.

safer

With the help of OopMap, Hotspot can do the GCRoots enumeration quickly and accurately. The problem is that if you have to store every instruction, the space is a little too large 💥, the cost of garbage collection will increase exponentially. Did the virtual machine lie? Another torture, the virtual machine cried: I did not make OopMap store ah, I just store in a specific location, such a location is called “safe point”.

So we found the safe point. The safe point said: the boss told me that when the user program is performing garbage collection, the code instruction stream must reach me before it can pause garbage collection. (You are not the point). Then I asked the collector, and the collector said, “This card can’t be too much, too much I wait too long, too little. I do it too often, and I load up the runtime memory.

What kind of place would create a safe spot? Of course, it is a busy place, the busy place road is particularly much, the program run time is also very long, “long time to execute” the most obvious feature is the instruction reuse, such as: method call, loop jump, abnormal jump, etc.

Well, we know Java is multi-threaded, so how can I get so many threads to run to the nearest safe point during garbage collection? Here we have two methods: Preemptive Suspension and active Suspension.

Thread interrupt scheme

Preemptive interruption

When the garbage collection needs to interrupt a thread, the system will interrupt all the lines first, which is to power off, and then see who is not at the safe point, resume the thread execution, and then a period of time to interrupt, until the safe point.

Active interrupt

Wrong when garbage collection need to interrupt threads, thread, simply set up a sign, all threads to take the initiative to go to polling, when found the result is true, is himself in the nearest safe point active hangs, polling operating instructions must be compact enough, because the polling operation will occur frequently in the code, so as to ensure its efficient enough. In HotSpot, this operation uses a memory-protection trap to reduce assembly instructions to one.

The safety area

We mentioned earlier that thread execution can run itself to a safe point, but what if the thread doesn’t? Huh? And when the thread doesn’t execute? Is the thread asleep?? Yes, it is to fall asleep 😴, that virtual machine should wait for it to wake up? Reallocate processor time? This is obviously impossible. The virtual machine refuses, so we need to introduce a Safe Region to help us fix it.

Points move into lines, lines move into planes. Safe point stretching naturally forms a safe zone, which ensures that reference relationships within a code snippet do not change.

This means that if a thread enters this area, it will be identified as having entered the area. When the vm performs the root node enumeration operation, it must wait until it receives the signal that the operation is completed before leaving.

📢 digression

😩 I find that where do I have time to exercise?

Did the black hole have too much time? In fact, not every day 20:00 or so home, have a meal, entertainment a little meeting, and then write an article to write fast 22:50 or so, and then 23:00, also exercise a ghost?

Just like working out, keeping track of time is also a difficult thing. Since I started keeping track of time, IT seems that I haven’t lasted more than a week. How did I keep track of time before? It is a simple record of the time I spend doing things every day. The table records: going to work, reading, writing, playing. The next day, I still go to work, reading, writing, playing.

I think time recording is too simple. As mentioned in the preface, time recording is a complex and complete system, which requires clear recording principles, appropriate time classification, simple recording behavior, thinking on recording efficiency and in-depth exploration on how to analyze and utilize time.

Did not look deeply, first when their own learning, in practice slowly explore it, so, Ming son to see 😂.