Python Memory Management and Tips Transcripts
Chapter: Recovering memory in Python
Lecture: GC without containers?

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's look at our GC example just one more time. There's one thing that I think is worth exploring on its own.
0:08 you'll recall, what we did is we had these two objects here, we create these two doomed and we said they represent people and people have friends.
0:16 So what we're gonna do is we're gonna say person1 is a friend of person2, and person2 has person1 as a friend. And then we're gonna blank them out,
0:24 and because these references point to each other, we saw that they're not going to be deleted or cleaned up by reference counting,
0:31 they have to use garbage collection. So over here, we had put this back the way it was. Here we go. We had this and we did a bunch of allocations here,
0:41 created a list, and by doing all this allocation, we created enough objects that the garbage collector had to run, so between step 2 and
0:50 3, you can see these doomed objects were collected, the cycle was detected, they were GC'd, everything was good. That seems straightforward,
0:59 right? Let's make a very small difference here. Let's suppose instead of creating things that can contain other things, like lists and so on,
1:07 well, we're gonna do is we're gonna go down here, and actually that may make a copy so you have both in the source code to play with.
1:16 We're gonna go down here, and we're just going to allocate some variables. We're gonna allocate about the same amount of stuff here.
1:26 We're gonna create X, which is whatever the range is squared, and then we're gonna create a string of it.
1:32 And then we're gonna create, replicate that 100 times within that string and store that in Y. Now, never mind that it says it's not used in PyCharm.
1:42 It's still used as far as the garbage collector is done until we get to the end of this function, okay? so, let's run it. Wait a minute.
1:50 What just happened? Look where the stuff got cleaned up. For some reason, even though we're doing basically the same stuff, I mean really,
1:59 really similar as before, we're not getting a garbage collection triggered. Why is that? Well, there's something super subtle going on here.
2:10 Over in this version, we're creating container objects. Lists. We created a class that would work. If we created
2:17 say, a tuple, it could contain other things. What we're doing here is we're creating objects, here a list, which has a bunch of strings and so on,
2:26 and then we're putting it in the list that technically doesn't really matter.
2:30 But we're creating objects that could potentially have cycles. Like a list containing an item
2:35 which could then point back to the other list and things like that, right? These container objects: dictionaries,
2:42 sets, tuples, classes and so on, they potentially can contain other objects so they
2:48 potentially could contain a cycle. But down here in this one instead, we have numbers, we have an "i" which is a number between 1 and 1000.
3:01 We have "X", which is the square of that number. We have a string created from X, a temporary one, and then it's created,
3:10 replicated 100 times, and we store that in "y". Can any of those hold other objects? Create a cycle to them? No. They're all immutable, actually.
3:20 Right? So the things we're creating are not of interest to
3:24 the garbage collector. So when we talk about allocation and the garbage collector even paying
3:29 attention to it, what you're going to see is that the garbage collector doesn't track everything that Python works with or does.
3:36 The garbage collector pays attention to container objects: dictionaries, lists, classes and so on,
3:43 Okay? So the things that are even eligible for garbage collection are a massive subset of what you generally work with in Python,
3:50 and it's super important to realize what triggers and interacts with the garbage collector and what doesn't.


Talk Python's Mastodon Michael Kennedy's Mastodon