复制垃圾收集器如何确保复制时不访问对象? [英] How does a copying garbage collector ensure objects are not accessed while copied?

查看:54
本文介绍了复制垃圾收集器如何确保复制时不访问对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在收集时,垃圾收集器将所有活动对象复制到另一个内存空间中,从而丢弃进程中的所有垃圾对象.将指向新空间中已复制对象的前向指针安装到该对象的旧"版本中,以确保收集器正确更新该对象的所有剩余引用,并且不会错误地将同一对象复制两次.

On collection, the garbage collector copies all live objects into another memory space, thus discarding all garbage objects in the process. A forward pointer to the copied object in new space is installed into the 'old' version of an object to ensure the collector updates all remaining references to the object correctly and doesn't erroneously copy the same object twice.

这对于停止世界收集者显然非常有效.但是,由于世界停止时的暂停时间很长,因此,当今大多数垃圾收集器都允许mutator线程与收集器并发运行,只停止一小段时间即可进行初始堆栈扫描.

This obviously works quite well for stop-the-world-collectors. However, since pause times are long with stop-the-world, nowadays most garbage collectors allow the mutator threads to run concurrently with the collector, only stopping the mutators for a short time to do the initial stack scan.

那么收集器如何才能确保在复制对象时/复制后,对象无法访问对象的旧"版本?我以为变异器可以使用某种读取屏障来检查前向指针,但是这对我来说似乎很昂贵,因为变量被如此频繁地读取.

So how can the collector ensure that the 'old' version of an object is not accessed by the mutator while/after copying it? I imagine the mutators could check for the forward pointer with some sort of read barrier, however this seems to costly to me since variables are read so often.

推荐答案

您几乎需要使用读取屏障或写入屏障.您显然已经意识到阅读障碍,因此我不会尝试进入它们.

You pretty much need to use a read barrier or a write barrier. You're apparently already aware of read barriers so I won't try to get into them.

作家障碍之所以起作用,是因为只要您防止发生写操作,您就根本不在乎有人访问数据的旧副本还是新副本.您设置写屏障,复制数据,然后开始调整指针.复制完成后,您实际上并不在乎有人是读取旧数据副本还是新数据副本,因为写屏障可确保它们是相同的.调整完指针后,所有数据都将使用新数据,因此您将取消写障碍.

Writer barriers work because as long as you prevent writes from happening, you simply don't care whether somebody accesses the old or the new copy of the data. You set the write barrier, copy the data, and then start adjusting pointers. After the copy is made, you don't really care whether somebody reads the old or the new copy of the data, because the write barrier ensures they're identical. Once you're done adjusting pointers, everything will be working with the new data, so you revoke the write barrier.

使用页面保护位将内存区域标记为只读以在相当标准的硬件上创建写屏障已经完成了一些工作.至少在上一次研究时,这仍然处于概念验证阶段-可以运行,但是太慢了,无法实用.

There has been some work done with using page protection bits to mark an area of memory as read-only to create a write-barrier on fairly standard hardware. At least the last time I looked into it, however, this was still pretty much at a proof of concept stage -- working, but too slow to be very practical.

这篇关于复制垃圾收集器如何确保复制时不访问对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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