What is a compaction in Java GC?

喜欢而已 提交于 2020-07-07 05:50:38

问题


I read http://www.cubrid.org/blog/tags/Garbage%20Collection/ article which gives high level picture of GC in Java. It says:

The compaction task is to remove memory fragmentation by compacting memory in order to remove the empty space between allocated memory areas.

Should objects be moved into anther places in order to fill holes?

I think that objects are moved. If so that mean addresses are changed and so reference to that object also should be updated?

It seems too complicated task to find all back reference and update them...


回答1:


Yes, arbitrary objects are moved arbitrarily through memory, and yes this requires updating the references to those objects. One could also use indirection but that has various downsides and I'm not aware of any high performance GC doing it.

It's indeed somewhat complicated, but as far as GC optimizations go it's rather benign. Basic mark-compact works rather well and it basically just goes over all objects in address order, moves them to the smallest available address, and builds a "break table" as it goes which contains the necessary information (start address -> displacement) for quickly fixing up references, which it then does in a second pass. None of this requires information or bookkeeping beyond what any mark-sweep collector already needs (object types, locations of references, etc).

And when you move objects out of the nursery in a generational setting, you also know (roughly) where the old references are. You needed to know that to do a minor collection.



来源:https://stackoverflow.com/questions/24587255/what-is-a-compaction-in-java-gc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!