Practice: Garbage Collection
Q1. Is the collector on
Print whether automatic garbage collection is enabled right now.
Goal: Use gc.isenabled.
Q2. Turn it off and on
Disable the collector, print `gc.isenabled()`, enable it again and print `gc.isenabled()` once more.
Goal: Control the collector.
Q3. Set a threshold
Set the thresholds to (800, 8, 8) and print the result of `gc.get_threshold()`.
Goal: Tune thresholds.
Q4. Self containing list
Create a list `a` holding the number 1, append the list to itself, and print `a[1] is a`.
Goal: Create a cycle.
Q5. Cycle then collect
Disable the collector, create two `Node` objects pointing to each other, keep a weak reference to the first, delete both names, collect, and print whether the weak reference now gives None.
Goal: Free a cycle.
Q6. Weak parent link
Write classes `Parent` and `Child` where the child stores a weak reference to its parent. Create both, then delete the parent and print whether `child.parent()` is None.
Goal: Use weak links.
Q7. Break a cycle by hand
Two nodes point to each other. Set both links to None, then print the two links on separate lines.
Goal: Break cycles.
Q8. Print a self list
What does this print?
a = [1]
a.append(a)
print(a)Goal: Read a cyclic list.
Q9. Threshold round trip
What does this print?
import gc
gc.set_threshold(100, 3, 4)
print(gc.get_threshold())Goal: Read threshold values.
Q10. Enabled flag
What does this print?
import gc
gc.disable()
print(gc.isenabled())
gc.enable()Goal: Understand disable.
Q11. Alive after delete
What does this print?
import gc
import weakref
gc.disable()
class N:
pass
a = N()
a.me = a
r = weakref.ref(a)
del a
print(r() is None)
gc.enable()Goal: Reason about self cycles.
Q12. Alive after collect
What does this print?
import gc
import weakref
gc.disable()
class N:
pass
a = N()
a.me = a
r = weakref.ref(a)
del a
gc.collect()
print(r() is None)
gc.enable()Goal: Reason about collection.
Q13. No cycle no wait
What does this print?
import gc
import weakref
gc.disable()
class N:
pass
a = N()
r = weakref.ref(a)
del a
print(r() is None)
gc.enable()Goal: Compare with cycles.
Q14. Garbage list
What does this print?
import gc
gc.collect()
print(gc.garbage)Goal: Know gc.garbage.
Q15. Fix the lingering parent
Children keep their parent alive through a normal reference, so a parent is never freed without the collector. Change the code to use a weak reference, so the final line prints True.
import weakref
class Parent:
pass
class Child:
def __init__(self, parent):
self.parent = parent
parent.child = self
p = Parent()
c = Child(p)
r = weakref.ref(p)
del p
print(r() is None)Goal: Remove a cycle.
Q16. Fix the threshold call
This code is meant to set the thresholds but crashes. Fix it.
import gc
gc.set_threshold('700', 10, 10)
print(gc.get_threshold())Goal: Set thresholds.
Q17. Fix the wrong module
The program tries to collect garbage but raises an error. Fix it.
import gc
gc.collect_all()
print('done')Goal: Use the right function name.
Q18. Why cycles leak
Explain in one or two sentences why reference counting alone cannot free two objects that point to each other.
Goal: Explain cycles.
Q19. Why generations
Why does the collector use generations?
Goal: Explain generations.
Q20. When to call collect
Give one good reason to call `gc.collect()` yourself.
Goal: Use collect wisely.