How does a copying garbage collector ensure objects are not accessed while copied?

前端 未结 3 2384
死守一世寂寞
死守一世寂寞 2021-02-20 04:44

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 objec

3条回答
  •  故里飘歌
    2021-02-20 05:23

    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.

提交回复
热议问题