3

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...

gavenkoa
  • 45,285
  • 19
  • 251
  • 303

1 Answers1

5

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.

Community
  • 1
  • 1