The Mark-Sweep garbage collection algorithm is a two-phase tracing collector that identifies live objects by marking them from root pointers during the mark phase, then reclaims garbage by sweeping through the entire heap and adding unmarked objects to a free list; however, this non-moving approach causes heap fragmentation, which slows down allocation and can lead to allocation failures even when sufficient memory exists.
Mark-Sweep Garbage Collection: Algorithms and Implementation
Added:you welcome back in this video we'll start our discussion about GC algorithms and the first algorithm we're going to consider is the widely used in practice known as mark-and-sweep the marks will be the tracing collector that is as we know from the previous lectures it identifies alive objects instead of the direct garbage let's go ahead and allocate some objects on the heap so we can have a work in this case for our collector for simplicity we're going to use only one route point in the first object so we allocate the object a pointed by the root then we allocate the object be pointed from a we allocate object C point it let's say also from a the object D pointed from B and final of the object e pointed from C and then at some point let's say we have lost the connection from A to B object here this potentially makes the object be a garbage since it's not reachable from the root but not only the object B also its direct child the object D is also becoming a garbage so let's dive into details how the mark sweep algorithm actually identifies the garbage and reclaims it as the name suggests the marks would collector works in two phases the mark phase which is actually the trace for the live objects and the sweep phase which already a reclaims the garbage in order to track which objects are alive during the mark phase the marks with collector stores in the object header the mark bit it's just a boolean flag and if an object is alive to fly cassette let's take a look how the mark in phase actually finds their live objects so as usually we start analysis from the roots and reach the first alive object that is the object a which is guaranteed to be alive since it's pointed by throat then we follow the child pointers of a in this case is the only pointer to the object C which now is also identified as alive with the same success we followed the child pointer of C and Mark the object is alive and at this point and the mark phase is completed let's take a look at abstract code implementation of the mark super algorithm and focus on the mark base first so as we said we start from the roots and initialize our to do list and as long as we still have something in this to do list we check whether the object has already been marked and if not we mark it by said in the market and go deeper to the child pointers of the current object also adding them to our to-do list after traversing of this object graph all visited that is reachable objects will be marked okay let's take a look at the sweet face now in contrast with the mark phase which visited on a small portion of the hip choosing only their live pointers the sweep phase has to traverse the whole hip starting from the very low addresses and up to the end of the hip that is it considers every single object one by one increasing the scan pointer by the size of the previous object let's take a look at the details so the first object we check is the object a and which has the mark bit set this means the object is alive so we just reset its mark bit for the future collection cycles and proceed to the next object B now the object B doesn't have the market set which means it wasn't visited during the mark phase and hence is a garbage what this means is that the memory block occupied previously by B can now be reused in future allocations and for this the block is added to what is known as the free list that is the linked list of all three blocks currently available on the heap and we proceed to the object C the objects he has the mark bit so it's alive where is said the mark bit and proceed to the next object the object E is not alive and the same as the object B is added to the free list and finally we identified the object E as a life and just reset this Martin bit at this point the full collection cycle is completed and as you can see we have reclaimed two memory blocks previously occupied by the objects be empty let's get back to our code implementation and see the sweep phase so as we said we start and the very beginning of the hip and until we reach at the end of the hip we can see their each object we check whether the object was visited that is his mark bits is set and if so this object is alive we just reset this mark bit and proceed further otherwise the object was not marked and it means it's a garbage so we return it back to the allocator and as we have seen the mark sweep uses the free list allocator to track all available free blocks and again we proceed to the next object this depends on the implementation for example it might be increasing in to the P pointer by the size of the previous object or it could be an explicit next pointer from the object header as in this example and by the end of the sweep operation all the garbage will be reclaimed and will be reused in the future allocations let's briefly talk about the main features of the mark sweeper collector first of all it is a non-moving collector what it means is that all objects after the collection cycle stay at exactly the same position as they where priority collection cycle the same true for the free blocks they are not moved anywhere but added to the free list next the direct consequence from the first feature is that for the allocation we use exactly the free list abstraction and this means that to satisfy an allocation request we have to first find the block of a needed size Traverse in the linked list of the three blocks and this is exactly why the heap allocation is in general considered slower than the stack allocation and finally what is also the consequence from the previous two features is the heap fragmentation notice again that the free blocks estate also the same position and such fragmented heap is the source of slower allocation and potential allocation failures when overall combined we would have enough space on the heap but because of this fermentation will not be able to find a chunk of a needed sighs okay here's a small summary of the mark Swift collector let's do a quick recap first it is a tracing collector which means it has juicy pauses and has a trace interface to identify a live objects it has two phases the mark phase which is the actual trace and to this whip phase which reclaims the garbage as we mentioned it is a non-moving collector which means it can be used by the languages which expose the point or semantics such as C or C++ this is very important as the objects stay at exactly same position for allocation it has to use however the free list abstraction which may slow down the allocation requests and finally what is the main issue of this collector is the external heap fragmentation which is bad for allocation speed and for cache locality okay now when we know how mark soup collector works we shall see how we can actually fix this fragmentation problem this is achieved by the mark compact and copying collectors which we are going to discuss in the next lectures
Up Next

Tri-Color Marking Process in JavaScript Garbage Collection
@DecodeWebWithRohan
317 views•2024-04-07

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

HTTP Requests Explained: GET, POST, PUT, DELETE
@codecademy
103.1K views•2021-10-07

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science
























![dr. M. Sojka, doc. D. Šišlák: Effective Software (BE4M36ESW) – 14 [19. 5. 2025, LS 24/25]](https://i.ytimg.com/vi/fLAg9B0B21I/maxresdefault.jpg)













