This is the 25th day of my participation in Gwen Challenge

Overview of garbage Collection concepts (Part 2)

4. Parallelism and concurrency of garbage collection

parallel

  • Parallelism: When multiple garbage collector threads work in parallel while the user thread is still waiting.
  • Serial: Single-threaded execution as opposed to parallel.

concurrent

  • The user thread and the garbage collector thread execute simultaneously (not necessarily in parallel, but alternately preemptively), and garbage collection does not suspend the user thread.

5. Safety points and areas

Safe Point

Program execution does not stop for GC in all cases, but only at specific points, called safe points. Setting too few safety points can lead to too long GC waits, and if setting too many leads to frequent GCS affecting program performance. It is best to set the safety point according to the characteristics of the program can be executed for a long time as the standard, such as: select a long execution time instruction as the safety point, method call, loop jump, exception jump, etc.

In the event of a GC, how do you ensure that all threads run to the nearest safe point of pause?

  • Preemptive interrupt (deprecated) : Interrupts all threads first, then restores them if they are not safe, allowing them to run to the safe point.
  • Active interrupts: Set an interrupt flag, which each thread polls for when it reaches a safe point, and suspend itself if the interrupt flag is true.

Safe Region

The safe point mechanism ensures that a program execution will not be too long before it hits a safe point to enter the GC. But what about when the program doesn’t execute? In cases such as blocking or sleep, where the thread cannot respond to the JVM’s interrupt request and cannot walk to a safe point to retrieve the suspend, a safe zone is required.

A safe zone is a section of code where the object’s reference relationship does not change, and where it is safe to start GC, or you can think of a safe zone as an extended point of safety.