[NSWI004] [nswi004] roots

Petr Tuma petr.tuma at d3s.mff.cuni.cz
Thu Oct 29 10:12:27 CET 2020


Hi,

> I hope it's possible for you to answer before the deadline for the graded quiz :).

Well, I was celebrating the national holiday debugging student assignments, I think I can do this too :)

> 1. I assume that roots are references (because how could there be an object in any hw register). Is my understanding correct?

Yes, that assumption kind of makes sense. As always, there are many details that could muddy up the picture - for example, I can imagine an object that only has a single integer field, in some parts of the code that field can be held in a register, which, factually, means the entire object is in a register :) but perhaps more importantly - we should realize that GC is _only_ interested in references, nothing else matters for the traversal purposes.

> 2. If roots are references, how does GC know if it is a valid reference or not ? Is it implementation specific of the language or there is some general technique ?

You can approach this from several angles. First, can our program store an invalid reference into a reference variable ? In most languages with GC, this is simply not possible by language design, hence your question does not arise. (And if it is possible, then there are solutions such as conservative GC.)

But perhaps what you meant was, when we can have roots in places such as registers - where integers and pointers simply look the same - how does the GC know when to include the content among roots ? The answer to this is, the compiler has to help. For every location in code where GC can happen, the compiler must emit enough information to tell the GC what registers hold references, the same goes for maps of stack content. (I mentioned this during the last lecture.)

Petr


More information about the NSWI004 mailing list