Models of Garbage Collectors

Models of Garbage Collectors

If you need a garbage collector, you can either install one or implement it by yourself. Which type of garbage collection should you use? A conservative garbage collector and a copying garbage collector are the two most common models.

A conservative garbage collector doesn’t move objects from one place to another. It uses constant-sized memory chunks (buckets). This way, the heap doesn’t become fragmented. The conservative model is more adequate for C++ because sometimes copying objects is undesirable or even impossible, especially if they contain pointers to other objects or data. A conservative garbage collector is also easy to implement. A copying garbage collector gives you more flexibility at the cost of higher runtime overhead. Under the copying model, memory blocks may have different sizes. As a result, heap fragmentation can occur. Therefore, a copying garbage collector must perform memory compaction periodically. During memory compaction, “live” objects are copied from their existing location to a contiguous memory block and their previous storage is recycled. Compaction is very expensive. Therefore, it’s less useful in time critical systems.


Share the Post: