Question: "How do you get to this scenario?"
In order to create a link from the old object to the young object, the old object must be accessible, since otherwise we cannot store the link to the young object in it. To subsequently become inaccessible, we need to change at least either: the root link or another old object that previously referred to the old object in question.
As already mentioned, such records are tracked by the JVM, which is crucial, because in order to find that the old object is now referencing the young object, it must know the memory area of the old object (aka map). Basically, since marking a map also means remembering incoming links, gc can now detect that the old object has become inaccessible, even without the main gc. He only needs to consider a modified map (or set of roots) to find out about it. Regardless of whether it works, it depends on environmental factors such as the selected gc algorithm, i.e. Whether you use CMS (wont) or G1 (possibly), and how "mixed collections" or actual memory pressure are configured.
Of course, if you use a parallel collector, there is a chance that a modification that will make the unreachable old object happen when the collection cycle is already ongoing. In this situation, it is possible that a young object is considered reachable during this gc cycle, even if it was not, if the modification occurred only a nanosecond earlier.
source share