Reputation: 491
Suppose we are using the mark-sweep garbage collection algorithm, if we are able to flag memory allocations as "reclaimable", isn't that enough? Wouldn't the program know that "reclaimable" memory is basically unused memory that can be allocated when requested? What are the physical differences between a "reclaimable" chunk compared to an "unused" chunk as shown in this picture:
Upvotes: 0
Views: 554
Reputation: 120858
The sweep step is just "another" way of delaying the stop-the-world event. Of course, in theory this is a good thing. While marking, also keep track of this "free" space, where dead objects have been identified. Thus, you can use that information from these lists when you allocate next time.
The thing is that the heap is not "compact" when you use sweep
only, as such when there is not contiguous free space for allocation (based on these free lists information), a compaction phase will still be performed.
My last point is that CMS
is deprecated, no one supports in, so understanding what it does might be interesting, but not really valuable; as other GC's do things differently.
Upvotes: 1
Reputation: 5749
Marking - During the mark phase all objects that are reachable from Java threads, native handles and other root sources are marked as alive, as well as the objects that are reachable from these objects and so forth. This process identifies and marks all objects that are still used, and the rest can be considered garbage.
Sweeping - During the sweep phase the heap is traversed to find the gaps between the live objects. These gaps are recorded in a free list and are made available for new object allocation.
Unused vs Reclaimable space - Unused space is nothing but gaps between Alive blocks, these gaps are created due to garbage-collection of unused/reclaimable objects, compaction stage will move unused blocks to the end. Compare two diagrams from the posted image.
Upvotes: 1