How does Java manage Root objects from the stack in G1 garbage collector? Does it traverse the stacks of all the mutator threads while garbage collecting(young or mixed phase) or is there some other data structures similar remember sets (used to keep the reference of intergenerational pointers) to save time? Is there any documentation of the same.
-
2As far as I know, it just scans the stack. Caching outgoing references would not yield much, given that stacks are expected to change between garbage collection cycles (as without mutator thread activity, there is no reason to perform another gc at all). – Holger Aug 05 '20 at 12:47
1 Answers
Thread stacks are always scanned, not only in G1
; but afaik in every other garbage collector implemented on the JVM. A GC
has to start with some known roots in order to find out what is alive and what not. In case of any java's GC, these roots are made from various pieces, among others : thread stacks.
At every cycle, these are scanned; after all they will be changed and will be different potentially at each cycle. The painful part is that until this is implemented, this is a stop-the-world phase. And STW
phases can get big due to safe point polling or number of threads. In real life scenarios (at least the ones that I have been involved with), this is not a concern; it's a fast process (with Shenandoah 2.0
, I have seen max of 15 ms
pause).
remembered sets
fulfill a somehow different role.

- 117,005
- 15
- 201
- 306