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

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

问题描述

现代垃圾收集器(如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中,尽管我不太了解其内部结构),该字节码是用对象vs基本信息键入的.结果,字节码中有数据结构,这些数据结构描述了每个堆栈帧中的哪些变量是对象,哪些是原语.当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 收集器.有保守的收集器,例如 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天全站免登陆