How does a garbage collector know what to delete?
Kernel Seed
0:00 / 0:00
How does a garbage collector know what to delete?
8 просмотров · 2 нед. назад
Kernel Seed
7 подписчиков
8 просмотров · 2 нед. назад
A hundred thousand objects, a thousand bytes each, none of them ever freed, and memory stays flat.
Two ways to find garbage, one idea underneath: the collector can't know what your program will use next, so it frees what can no longer be reached. We start from a small heap of objects and arrows, count references until one reaches zero, watch two objects keep each other alive in a cycle, then mark from the roots and sweep what was never marked. The video also covers the price of tracing (pauses), the generational idea that most objects die young, and why a list that only grows is a leak no collector can see.
Simplifications: the heap is six objects with the arrows drawn by hand. Concurrent and incremental collectors are mentioned, not explained. Generations are shown conceptually, without any one runtime's details. CPython is the only runtime named: it uses reference counting and adds a cycle collector. The numbers on screen come from one run of our own Python script.
Sources
Python docs, Reference Counts (the object is freed when the count reaches zero) — https://docs.python.org/3/extending/e...
Python docs, gc: Garbage Collector interface (cycle collector, gc.disable, gc.collect, generations) — https://docs.python.org/3/library/gc....
D. Ungar, "Generation Scavenging" (1984) — https://people.cs.umass.edu/~emery/cl...
Chapters
0:00 Memory stays flat
0:21 What garbage means
1:13 Reference counting
1:52 The cycle
2:26 Mark and sweep
3:27 The cost: pauses
3:58 Generations
4:42 The leak GC can't see
5:18 Back to the heap