Python Memory Management and Tips Transcripts
Chapter: Recovering memory in Python
Lecture: Pythons generational garbage collector

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