Memory management in Python

Everything is Python is object. And Python uses Dynamic memory allocation.

Every object in Python has a reference count and a pointer to a type, and value.

Variables in Python are just the references to the objects in the memory.

image

Memory Allocation in Python

There are two parts of memory:

  • stack memory - all methods and their variables are stored in the stack memory. Done during compile time. Stack contains references to objects.
  • private heap memory - all objects (data structures) and instance variables are stored in the heap memory. When varaiable is created it is stored in private heap, and accessible globally by all program methods. It is managed by Python Memory Manager. The goal of the memory manager is to ensure that enough space is available in the private heap for memory allocation. This is done by deallocating the objects that are not currently being referenced (used). After the variable is returned, the Python garbage collector gets to work.

Python has three different levels when it comes to its memory structure:

  • Arenas (biggest, 256KiB) - responsible for memory allocating. It is optimized for small object ≤ 512 bytes.
  • Pools - arenas can be broken to 64 pools. Each pool's size = 4Kb (memory page size) and it can have three possible states:
    • Empty, used, full
  • Blocks - pool can be broken into blocks, size of a block ranges from 8 to 512 bytes and must be a multiple of eight.
    • Untouched (not allocated yet), Free (was allocated but now it is free), Allocated

For all this job Python Memory Manager is responsible. If the objects get destroyed, the memory manager fills this space with a new object of the same size.

Large objects are directed to the standard C allocator within Python.

References

SuperMade with Super