Python Memory Management and Tips Transcripts
Chapter: Recovering memory in Python
Lecture: Pythons generational garbage collector
0:00 We've seen this mysterious GC, this mysterious garbage collector in action, but we don't really know what triggers it. We saw that it somehow has to do
0:10 with container objects versus non container objects like lists and dictionaries rather than strings and numbers.
0:16 So what we're going to do in the next couple of little sections here is
0:20 talk about how this works. So Python has what's called a "Generational Garbage Collector",
0:25 and this is a really important optimization that most modern garbage collectors have.
0:31 And the way it works is all the objects start out in was called "Generation Zero". It talks about how many times have they been collected, in this case
0:39 never, and then once an object has been inspected but not deleted, it was inspected, and it decided that it was still alive
0:48 for whatever reason, it's promoted into Generation one, and then sometimes we'll see about 1/10 of the time,
0:56 the stuff in generation zero and generation one will be compared. Most the time we'll just focus on Generation zero,
1:02 and that's because the overwhelming pattern is that new, temporary little things are created for a moment and then thrown away. So objects will live
1:10 very briefly. So typically it's enough to just look in generation zero,
1:14 but we'll see the mechanism by which Python determines it needs to look broader.
1:18 So it'll look at maybe generation one and zero items if it needs to look a
1:23 little broader and if it finds something in generation one that is still valid, but it has been inspected twice,
1:31 It's now sent to two, and same thing applies. Sometimes Gen 1 and 0 will be inspected,
1:37 but rarely will Gen 2. So these things are even older and even more
1:42 infrequently inspected, and we're gonna talk about the ratios and how you can see and configure that and all in just a minute.
1:49 But by default, things start out in generation zero. If they happen to survive a garbage collector
1:56 run, an inspection, then they get promoted and they get inspected less often. If they survive a second or further time,
2:03 they get promoted to generation two, and over in generation two, they don't get inspected nearly
2:08 as often. Remember, reference cycles only occur in container objects: lists, dictionaries, classes and tuples, and so on. For that reason,
2:18 only those objects are subject to Python's garbage collector, right? We saw that we worked with strings and numbers and so on,
2:26 No problem. They did not interact or trigger the garbage collector. When you think about all the stuff and all these algorithms,
2:33 it's easy to think "Well, every little thing I do in Python has this applied to it on top of reference counting". No, just container objects.
2:40 So that's worth keeping in mind, too.