- Question posed: Does GC for the JVM really happen at any time?
- No, only STW (stop the world) will be executed when the user thread stops. There is. It’s a safe spot and a safe zone here.
1. What is a Safe Point
Program execution does not always pause to begin GC, but only at specific points, called “safepoints.”
2. Safe spot selection
The choice of safe points is important because too few can lead to long GC waits, and too frequent can lead to performance problems at runtime. The execution time of most instructions is very short, and is usually based on “characteristics that make the program run for a long time”. For example, select some commands that take a long time to execute as safe points, such as:
- End of loop
- Method before return
- After calling the method
- The location of the throw exception
3. Interrupt policy of thread during GC
How do you check that all threads run to the nearest safe point and stop during GC life?
- Preemptive interrupt: (no virtual machine currently uses it) interrupts all threads first. If there are still threads that are not at the safe point, restore the thread and let it run to the safe point.
- Active Interrupts: Sets an interrupt flag, which threads actively poll when running to Safe Point, and suspends themselves if the interrupt flag is true. (The inn door has a monitor that says true, or false. If the system needs garbage collection, it updates the status to true, and when the thread gets to the inn, it sees true and enters the inn.
4. Safe Region
- The SafePoint mechanism ensures that when a program executes, it will not take too long to encounter a SafePoint ready for GC. But what about when the program “doesn’t execute”? For example, when a thread is in the state of Sleep or Blocked, unable to respond to interrupt requests from the JVM, the thread “walks” to a safe point to interrupt the suspension, and the JVM is less likely to wait for the thread to wake up. In this case, a Safe Region is required.
- A safe zone is a code snippet where the reference relationship of the object does not change and it is safe to start GC anywhere in the zone. We can also think of Safe Region as Safepoint extended
5. Safe Region- Execute the flow
When a thread runs into Safe Region code, it first identifies that it has entered the Safe Region. If GC occurs during that time, the JVM ignores the thread identified as Safe Region: When the thread is about to leave, it checks to see if the JVM has completed GC, and if so, continues running, otherwise the thread must wait until it receives a signal that it is Safe to leave the Safe Region.
- In a word, sleep is ok, please enter the hotel before sleeping, and enter the screen to say I have entered the safe area, sleep. The purpose of this is not to find you when you are going to GC. If you see your thread name on the screen, you will know that you are safe and will ignore you.
- You (thread) wake up and go out. Look up to see if it is safe to go out (true), if not (false) stay in the hotel, do not go out, wait until the GC is complete, the status changes to False to go out.