Memory Arenas
00:00
Arenas are at the highest level of abstraction, and they contain pools. However, they don’t have explicit states in the way that pools and blocks do. Arenas are instead organized into a doubly-linked list called usable_arenas
.
00:18 This is like the starting point of CPython’s journey to find some available memory. This doubly-linked list allows the allocation algorithm to see which arena has the least amount of overall free space.
00:31 That’s where it will try to allocate new blocks of memory first.
00:36 This seems kind of counterintuitive. After all, why not try to allocate new blocks in the arena with the most space available? The reason for this is reduced memory footprint.
00:47 If an arena contains all free blocks, then that entire arena—remember, 256 kilobytes—can be truly freed. CPython will release that memory back to the operating system.
01:01 This is why the allocation algorithm prioritizes allocating free blocks over unused ones. The whole idea is to try to use memory that’s already been used, in an attempt to increase the likelihood of one of these mostly free arenas eventually containing all free pools.
01:20 Then, that arena can be freed and Python’s overall memory usage on your computer will be reduced. It may seem kind of crazy, but there is a method behind all this madness.
Become a Member to join the conversation.