In the mark-and-sweep algorithm, the sweep part of the algorithm defragments the heap by moving live objects next to each other. This sliding of the objects requires a lot of tracking. If your heap is small enough, it is more efficient to just copy all live objects to a new location instead of reusing your current heap.
This is what a copying collector does. Take your heap and split it in half. You use one half as your real heap. When you GC, you move all live objects into the other half of the heap. You now start using the other half of the heap. Every GC cycle you switch which half you use.
The main disadvantage of a copying collector is the amount of memory it uses. It essencially doubles how much heap space you need. This is where the generational garbage collector comes in handy. You can use the copying collector on the young generation since it is small. This makes the frequent GC cycles that much faster.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.