/  Technology   /  Python   /  How is memory managed in Python?
Python Memory Managment(i2tutorials.com)

How is memory managed in Python?

A private heap is involved in the Python memory management that contains all python object abs data structure. This private heap is managed by the Python memory manager. The Python memory manager deals with different dynamic storage management aspects like segmentation, sharing, reallocation. Most importantly the management of the Python heap is activated by the interpreter itself and the user has no control over it but when they regularly manipulate abject pointers then the memory blocks inside the heap. The allocation of the heap is performed by the python memory manager through python/C API for the python object and other internal buffers.

At the lower level, for the storing of the python related data, the space in the private heap is ensured by a raw memory allocator. On top of it, several object allocator operates on the heap and implement distinct memory management policies for the different object type.


The heap manages an integer object in a different way than strings, tuples because integer needs specific storage requirement and speed or space trade-off. Python memory manager represents some of the work to the object-specific allocators but ensures that the latter operate within the private heap.

To avoid memory blockage. They never try to use python object with the function in the C library. As a result, they will prepare the mixed call in the C allocator and the Python memory manager with the fatal effect because they produce different algorithm and operate the system.

It is recommended that the allocated memory from the Python heap because in latter it is under control to the Python memory manager. Another reason to use the Python heap, when the interpreter is extended to the new object type then the Python memory manager needs to informed about the memory needs for the module. When the requested memory is used internally then the memory manager has the appropriate image of the memory footprint. Python memory manager may be or may not be activated appropriate action like memory compaction and another preventive method.

Leave a comment