

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 9
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
Click to play video
Now that we've done the first (and simplest) part: marking, we can trace through all of our objects and determine which ones are connected to the roots. For example:
def get_list():
a = 5
return [a]
print(get_list())
If we run this code it will return a list with the integer a inside of it. Our current mark function will mark the list, but it won't mark the integer a. Which means that when we go to sweep the memory, we will mark the list, but not the integer a. We'd then free a while it's still being used, and the operating system could then fill that memory with something else... which would be very bad!
So we need to prevent this!
But we also have another problem:
def get_list():
a = []
a.append(a)
return [5]
print(get_list())
In the above (very dumb) example, we create a list that references itself and then return a completely unrelated list.
If our trace function looks for any object that is referenced by any other object it will consider a alive because it has a reference (albeit, to itself in this case). In fact, a is unreachable because when get_list returns, a is no longer used anywhere.
Tracing solves these problems. To be clear, tracing is part of the "mark" phase of mark and sweep. It's where we mark all the objects referenced by our root objects.
This is one of the larger assignments, take your time. You're almost done with the course!