Garbage collection
Every object a program makes takes memory, and that memory has to be given back. In C you do it by hand with free(), and mistakes cause leaks or use-after-free bugs. Step through eight lines of Python to watch CPython count references and free a list the moment its count hits zero, see how a reference cycle slips past the counts, and then watch a tracing collector mark what the roots can reach and sweep the rest.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01In CPython, after a = [7] then b = a then del a , what happens to the list?
del removes a name, not the object. The list still has one reference, from b, so its count is 1 and it stays in memory. CPython frees it only when its count reaches 0.
02Two lists refer to each other, and no variable refers to either. Why does reference counting alone not free them?
That is a reference cycle. The program cannot reach them, but each count stays at 1. CPython's cycle detector in the gc module finds and frees such groups.
03In mark-and-sweep, which objects survive the sweep?
The collector marks everything it can reach by following references from the roots (the variables still in use). Anything left unmarked is garbage and is swept, cycles included.
04Why can a simple mark-and-sweep collector make a program pause?
A simple collector stops the program while it traces and sweeps: a stop-the-world pause. Concurrent collectors, such as Go's and Java's ZGC, do most of that work alongside the running program, so the pauses stay short.
05In C, what is a use-after-free bug?
After free(p) the memory may be reused for something else, so reading or writing through p is undefined behaviour. Forgetting free() altogether is a different bug: a memory leak.
Glossary
- Heap
- The area of memory that holds objects a program creates while it runs, such as lists and dictionaries.
- Memory leak
- Memory that a program no longer needs but never gives back, so it stays used until the program ends.
- Use-after-free
- A bug in languages with manual memory, such as C: using memory after it has been returned with free().
- Reference
- A way to reach an object: a name bound to it, or a slot inside another object, such as an item in a list.
- Reference count
- The number of references that point at an object. CPython frees the object when the count reaches 0.
- Reference cycle
- A group of objects that refer to one another, such as two lists that each hold the other.
- Roots
- The references a program can use directly, such as its variables. A tracing collector starts from them.
- Mark-and-sweep
- A tracing collector: mark every object reachable from the roots, then free every object left unmarked.
Notes and sources
- sys.getrefcount() reports one more than you might expect, because passing the object to the function adds a temporary reference. The counts in the figure subtract that one.
- Reference counting is how CPython, the standard Python, works. Other Pythons differ: PyPy, for example, uses a tracing collector, so an object is not always freed the moment its last reference goes.
- CPython's cycle detector only looks at container objects (lists, dicts, class instances and the like), because only they can form cycles. It runs automatically when enough new objects have been made since the last run; gc.collect() runs it on demand.
- Real tracing collectors are more refined than plain mark-and-sweep: Java and Go use generations, compaction or concurrent marking. The idea underneath is the same: whatever cannot be reached from the roots is garbage.
- In C, using memory after free() is undefined behaviour: the program may seem to work, crash, or quietly corrupt other data.
- Python reference: Data model, objects and garbage collection
- Python library: gc, the garbage collector interface
- Python library: sys.getrefcount
- Python C API: Reference counting
- Python reference: The del statement
- cppreference: free (C)
- MDN: Memory management (mark-and-sweep)
- Go: A Guide to the Go Garbage Collector
- Oracle: HotSpot Virtual Machine Garbage Collection Tuning Guide (Java 21)
Quiz Unlocks after the last step.