How do garbage collectors know about references on the stack frame?

后端 未结 3 1180
走了就别回头了
走了就别回头了 2021-02-04 13:37

What techniques do modern garbage collectors (as in CLR, JVM) use to tell which heap objects are referenced from the stack?

Specificall

3条回答
  •  太阳男子
    2021-02-04 13:47

    In Java (and likely in the CLR although I know its internals less well), the bytecode is typed with object vs primitive information. As a result, there are data structures in the bytecode that describe which variables in each stack frame are objects and which are primitives. When the GC needs to scan the root set, it uses these StackMapTables to differentiate between references and non-references.

    CLR and Java have to have some mechanism like this because they are exact collectors. There are conservative collectors like the boehm collector that treat every offset on the stack as a possible pointer. They look to see if the value (when treated as a pointer) is an offset into the heap, and if so, they mark it as alive.

提交回复
热议问题