CPython's Memory Management
00:00 You’ve learned why memory management is important and what process is responsible for doing it. You’ve also seen how the GIL acts as a safeguard to ensure memory is not mishandled due to multithreaded race conditions.
00:15 The rest of this course will focus on how CPython utilizes memory when our program needs it. Before we do that, some vocabulary. I’ve used some of this throughout the course, but I want to make it very clear what I’m talking about here. To allocate memory is to make the memory accessible.
01:30 Everything else is not accessible to CPython. Part of CPython’s allocated memory is used for internal purposes that have nothing to do with your code specifically. The other part is object-specific memory.
01:50 However, collection types like the list or the dictionary, can contain objects of any type. And so for that, CPython has an object allocator that is responsible for allocating memory within the object memory area.
02:06 This object allocator gets called every time a new object needs space allocated. Because most memory allocation involves dealing with small amounts of data—for example, changing a string variable or adding a new integer to a list—it’s tuned to work well with small chunks of data.
02:45 These are the largest fixed-sized chunk of memory aligned on a page boundary in memory. A page boundary is the edge of a fixed-length contiguous chunk of memory that the OS uses. Python assumes the system’s page size is 256 kilobytes, so that’s how big each arena is. The arena is like the page you see when you open the book.
03:11 Arenas are made up of pools, where each pool is 4 kilobytes in size. Inside a pool are blocks, which can be allocated to store data. Pools each have a size class, which states how big each block inside can be. For example, this pool on the left has eight blocks, each of which stores roughly 500 bytes of memory.
03:58 If a request for between 1 and 8 bytes of memory is needed, a block in the 8-byte pool will be allocated. If a 36 byte request comes in, a 40-byte block will be allocated. Next, we’ll take a look at how each of these components work in more detail, starting with pools.
Become a Member to join the conversation.