Python Memory Management and Tips Transcripts
Chapter: Allocating memory in Python
Lecture: Allocation in action

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's do a little thought experiment. Imagine we have this one line of Python code, which we know whole tons of stuff is happening down in the runtime.
0:08 But on the Python side, it's simple. We have a person object. We want to create them, past some initial data over
0:13 to them, their name is Michael, and so on. Now let's imagine that to accomplish this operation we need 78 bytes of RAM from the
0:24 operating system. What happens? How does that get into Python? Like, what part of memory do we get? How is it organized and so on?
0:33 So a very simplistic and naive way to think about this would be all right, what we're gonna do is going to go down to the C-layer, and C-Layer
0:41 is going to use the underlying operating system mechanism for getting itself some memory.
0:47 It'll malloc it, right? So malloc is the C allocation side and then free, we would call free on the pointer to make that memory go away,
0:55 Okay? So you might just think that the C runtime just goes to the operating system and says "give me 78 bytes", and the operating system says "super,
1:04 we're gonna provide 78 bytes of virtual memory that we've allocated to that process",
1:10 which then, boom into that we put the thing that we need, some object that has an id and the id wasn't explicitly set. But, let's say it's generated.
1:19 The id is that in the name is Michael. Well, that seems straightforward enough. I mean, you have these layers, right?
1:27 Python is running and then Python is running actually implemented in C and C is running on top
1:32 of the operating system and the operating system is running on real RAM on top of hardware. So this seems like a reasonable thought process.
1:40 But no, no, no. This is not what happens. There's a whole lot more going on. In fact, that's what this whole chapter is about,
1:47 is talking about what happens along these steps. And it's not what we've described here. Let's try again. So at the base we, of course,
1:57 still have RAM. We have hardware. That's where memory lives. We still have an operating system.
2:02 Operating systems provide virtual memory to the various processes that are running so that one process can't reach in and grab the memory of another.
2:10 For example, we saw that there's ways in the operating system to allocate stuff.
2:16 So at C, there's an API called malloc that's gonna talk to the underlying operating
2:21 system. This is what we had sort of envisioned the world to be before. But there's additional layers of really advanced stuff happening on top here.
2:30 Above this, we have what's called Pythons allocator or the PyMem API and PyMalloc. So the C runtime doesn't just call malloc,
2:40 it calls PyMalloc, which runs through a whole bunch of different strategies to do
2:45 things more efficiently. We saw that in Python and CPython in particular, that every tiny little piece of data that you work with, everything, numbers,
2:54 characters, strings, all the way up to lists and dictionaries and whatnot,
2:59 these are all objects, and every one of them requires a special separate allocation, often very small bits of data, and that's why Python has this
3:07 Pymalloc. But wait, there's more. If you're allocating something small, and by small, I mean sys.getsizeof,
3:17 not my fancy reversal thing. So if you're allocating something that is in its own
3:22 essence small, then Python is going to use something called the "Small Object Allocator",
3:28 which goes through a whole bunch of patterns and techniques to optimize this further, and we're going to dig into that a bunch.
3:35 So if you want to see all this happening, you can go to "bit.ly/pyobjectallocators", the link at
3:41 the bottom, and actually the source code is ridiculously well documented. There's like paragraphs of stuff talking about all these things in here,
3:50 but there's actually, in there, There's a picture, ASCII art-like picture that looks
3:54 very much like this diagram that I drew for you with some details that I left out, but they're in the source code.


Talk Python's Mastodon Michael Kennedy's Mastodon