Python Memory Management and Tips Transcripts
Chapter: Recovering memory in Python
Lecture: Ref counting in source

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, we've come to the end of the line.
0:02 Not for our course, there's a lot left there.
0:04 But for the life-cycle of Python objects.
0:07 We're going to talk about cleaning up memory and destroying Python objects that we've been working
0:11 with, but we don't need anymore.
0:14 This is really what most people think about when they think about Python Memory Management.
0:18 I think the stuff about allocation is not even on people's radar
0:22 for the most part, but this is. This is when we're working with objects and we're
0:27 working with data, how does Python make it go away without us having to do
0:32 what C has to do? Where you have accounting
0:35 and some part of the program is responsible for tracking when a thing can go away
0:38 and it's got to make sure it doesn't do it too early,
0:40 but not too late. So we're gonna talk about two different techniques in Python that,
0:46 but let's not worry about that,
0:47 but understanding it is still super important.
0:50 So if we grab our red pill again and jump over into the CPython source
0:54 code, you can just go to "bit.ly/cpythonobject" to go find this
0:59 bit here. Remember, that we saw object.c already.
1:02 This is object.h, the header file that defines what is one of these
1:07 objects. There's 667 lines just in the header.
1:09 This is a crazy thing. So I've only grabbed a very small bit of the
1:14 definition of PyObject. So we're defining a new type,
1:17 You can see at the bottom it's called "PyObject", we're gonna have a whole
1:21 bunch of pointers to it, but the most interesting thing I want you to take
1:24 away in this part is to do with this "ob_refcnt".
1:29 So how Maney references, how Maney variables, pointers,
1:34 remember, Python doesn't have pointers,
1:35 but it kind of does so,
1:37 references or pointers point back to this particular object?
1:42 and it's a "Py_ssize_t",
1:44 which is a weird type,
1:46 basically, it's a way to get an unsigned number,
1:50 an integer or a long or something like that, that matches the type of architecture you have.
1:56 So like on a 64 bit processor it's probably 64 bit or 64 bit version of
2:00 Python, a 64 bit number or a long, things like that.
2:06 Think of it is just a number,
2:08 an integer, and it just counts:
2:10 0, 1, 2, 3, 4, 5 as different variables point at this object,
2:15 Python behind the scenes will increment this number, and as they stop pointing at it,
2:19 cause either they go out of scope and get destroyed or they get assigned to
2:22 another variable or to none or something like that,
2:25 Python will take this down, and when that number reaches zero,
2:29 the object is cleaned up. Boom,
2:31 That's it. Remember, it's taken out of its block so that other things can
2:35 be put into that hole into that block and reused.
2:40 This is the primary way, this reference counting idea is the primary way that Python
2:44 keeps track of things that you're working with and cleans them up for you.
2:49 There's a secondary way, because reference counting does have some flaws.
2:53 But when you think of Python Memory Management and things getting cleaned up,
2:57 this should be the first thing that comes to mind.