Python Memory Management and Tips Transcripts
Chapter: Efficient data structures
Lecture: Disabling the gc revisited
0:00 I hope this article from Instagram about how they disabled Pythons
0:03 garbage collection and used less memory and not more made an impression on you.
0:08 It sure did on me when I first heard about it.
0:11 It honestly kind of blew my mind.
0:13 What was it that made this possible?
0:15 Why could they do this? Well,
0:17 let's go back to the python documentation. Right here in the center line bit, once again
0:21 it says "you can disable the collector if you are a sure your program does
0:26 not create reference cycles", and I would change that a little bit like "it doesn't create
0:30 too many reference cycles" because, you know,
0:33 if it's short lived and it creates a couple, who cares,
0:36 right? If it leaks a little bit of memory.
0:38 But if, like the core thing you're doing,
0:39 the core data you're working with require cycles,
0:42 well, I guess you can't do it.
0:45 So what we're going to do in this next section is we're gonna look at a
0:47 situation that has reference cycles and say "is there some way to restructure the algorithm so
0:53 we no longer have these cycles in our data?" And then it would be possible to
0:58 turn off the garbage collector. Not necessarily saying we have to do that,
1:02 but we're going to talk about these cycles and basically avoiding creating them,
1:07 using different algorithms and slightly different data structures.