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

前端 未结 3 2382
死守一世寂寞
死守一世寂寞 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:28

    Disclaimer: this is related to java's Shenandoah GC only.

    Your reasons are absolutely spot on for Shenandoah! Some details here for example.

    In the not so long ago days, Shenandoah had write and read barriers for all primitive and reference types. The read barrier was actually just a single indirection via the forwarding pointer as you assume. Since they are so much more compared to writes (which was a much more complicated barrier), these read barriers were more expensive (cumulative time-wise) vs the write ones. This had to do with the sole fact that these are so darn many.

    But, things have changed in jdk-13, when a Load Reference barrier was implemented. Thus only loads have a barrier, write happen the usual way. If you think about it, this makes perfect sense, in order to write to an Object field, you need to read that object first, as such if your barrier preserves the "to-space invariant", you will always read from the most recent and correct copy of the object; without the need to use a read barrier with a forwarding pointer.

提交回复
热议问题