Practice: Garbage Collection

« Read the chapter

Q1. Is the collector on

BeginnerCoding

Print whether automatic garbage collection is enabled right now.

Goal: Use gc.isenabled.

Q2. Turn it off and on

BeginnerCoding

Disable the collector, print `gc.isenabled()`, enable it again and print `gc.isenabled()` once more.

Goal: Control the collector.

Q3. Set a threshold

BeginnerCoding

Set the thresholds to (800, 8, 8) and print the result of `gc.get_threshold()`.

Goal: Tune thresholds.

Q4. Self containing list

IntermediateCoding

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

AdvancedCoding

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

AdvancedCoding

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

IntermediateCoding

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

BeginnerOutput

What does this print?

Code
a = [1]
a.append(a)
print(a)

Goal: Read a cyclic list.

Q9. Threshold round trip

BeginnerOutput

What does this print?

Code
import gc
gc.set_threshold(100, 3, 4)
print(gc.get_threshold())

Goal: Read threshold values.

Q10. Enabled flag

BeginnerOutput

What does this print?

Code
import gc
gc.disable()
print(gc.isenabled())
gc.enable()

Goal: Understand disable.

Q11. Alive after delete

IntermediateOutput

What does this print?

Code
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

IntermediateOutput

What does this print?

Code
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

IntermediateOutput

What does this print?

Code
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

IntermediateOutput

What does this print?

Code
import gc
gc.collect()
print(gc.garbage)

Goal: Know gc.garbage.

Q15. Fix the lingering parent

AdvancedDebugging

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.

Code
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

BeginnerDebugging

This code is meant to set the thresholds but crashes. Fix it.

Code
import gc
gc.set_threshold('700', 10, 10)
print(gc.get_threshold())

Goal: Set thresholds.

Q17. Fix the wrong module

BeginnerDebugging

The program tries to collect garbage but raises an error. Fix it.

Code
import gc
gc.collect_all()
print('done')

Goal: Use the right function name.

Q18. Why cycles leak

IntermediateConceptual

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

AdvancedConceptual

Why does the collector use generations?

Goal: Explain generations.

Q20. When to call collect

IntermediateApplication

Give one good reason to call `gc.collect()` yourself.

Goal: Use collect wisely.