垃圾收集器如何知道堆栈帧上的引用? [英] How do garbage collectors know about references on the stack frame?

查看:39
本文介绍了垃圾收集器如何知道堆栈帧上的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现代垃圾收集器(如在 CLR、JVM 中)使用哪些技术来判断堆栈中引用了哪些堆对象?

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

具体来说,VM 如何从知道堆栈开始的位置返回到解释所有对堆对象的本地引用?

Specifically how can a VM work back from knowing where the stack starts to interpreting all local references to heap objects?

推荐答案

在 Java 中(很可能在 CLR 中,虽然我不太了解它的内部结构),字节码是用对象与原始信息输入的.因此,字节码中有数据结构来描述每个堆栈帧中的哪些变量是对象,哪些是原语.当 GC 需要扫描根集时,它使用这些 StackMapTables 来区分引用和非引用.

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 和 Java 必须有这样的机制,因为它们是 exact 收集器.conservative 收集器,例如 boehm 收集器,它们会处理堆栈作为可能的指针.他们查看该值(当被视为指针时)是否是堆中的偏移量,如果是,则将其标记为活动.

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.

这篇关于垃圾收集器如何知道堆栈帧上的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆