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