Python Memory Management and Tips Transcripts
Chapter: Allocating memory in Python
Lecture: Small object allocation introduction

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's look at one of the red pills in CPython around object allocation.
0:04 So this is "obmalloc.c",
0:08 and if you look here, you can see here's the ASCII art part that I was
0:11 telling you about before. This is when the second take that we did on what allocation
0:16 looks like. We have physical RAM,
0:18 we have virtual memory, then we have malloc, then we have
0:21 PyMem, API, the allocator,
0:24 but there's something really interesting at the bottom that we didn't talk about then and check
0:28 that out. It says "a fast special purpose memory allocator for small blocks to be
0:33 used on top of a general purpose
0:35 malloc heavily based on prior art".
0:38 And if you want to go check this bit of the source code,
0:40 this is literally straight out of GitHub,
0:42 just go to bit.ly/pyobjectallocators,
0:46 and it will take you right to this line,
0:48 and you can look through it. So what's the deal? To reduce the overhead for small objects,
0:53 that is, objects that are less than 512 bytes in the
0:59 "sys.getobjectsize", not the whole traversal,
1:01 but the small bit, Python sub-allocates larger blocks of memory,
1:06 and then, as you need it,
1:08 will free up, or give, that memory to different objects,
1:12 which potentially could be reused once that object is cleaned up.
1:17 Larger objects are just routed to the standard malloc.
1:20 But for these smaller ones, which are most of them,
1:23 the Small Object Allocator uses these three levels of abstraction.
1:28 We've got arenas and pools and blocks. At the lowest level
1:31 we've got this thing called a block, and then pools
1:34 manage blocks and arenas manage pools.
1:37 So we're gonna go through all of those.
1:38 But there's this trifecta of ideas or algorithms that we're going to use to manage,
1:45 remember, the small objects. And this little quote right here comes from an article about
1:51 Python memory management by Artem Golubin,
1:54 and he's done some fantastic research and writing around it.
1:57 So I recommend that you check out his blog. There'll be a couple of articles that
2:01 I think I refer to. Definitely,
2:03 I've read as researching all the stuff for this course,
2:06 so check out his article here. It has a lot of interesting analysis
2:09 on what's happening and why it's being done.