

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 7
click for more info
Not enough gems
Cost: 6 gems
1: Handling Cycles
incomplete
2: Pros and Cons
incomplete
3: Stack Frames
incomplete
4: Tracking Objects
incomplete
5: Free
incomplete
6: Frame References
incomplete
7: Mark and Sweep
incomplete
8: Mark
incomplete
9: Trace
incomplete
10: Sweep
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Sweep is easy! Trace was probably the hardest part of the garbage collector.
Every object now has an is_marked field that we can use to determine if an object is reachable or not. All we need to do is iterate over all the objects in the VM and free any object that is not marked. Once it's freed, we can also remove it from our VM.
One more thing that's not obvious about sweep(): Any object that is marked (we don't want to free it right now) needs to be reset to is_marked = false. That way the next time the mark phase runs, if it's not marked again it will be freed in the next cycle.
Hope you liked the course! Great work.
You can use the snek_object_free function to free the objects