Current location - Health Preservation Learning Network - Healthy weight loss - Python memory management mechanism
Python memory management mechanism
For Python, memory management involves all objects and heaps that contain Python. Python memory manager internally ensures heap management and allocation. Python memory manager has different components, which can handle all aspects of dynamic storage management, such as * * * sharing, segmentation, pre-allocation or caching.

At the lowest level, the original memory allocator ensures that there is enough space in the heap to store all Python-related data by interacting with the operating system's memory manager. On top of the original memory allocator, several object-specific allocators run on the same heap and realize different memory management strategies suitable for each object type.

For example, the management of integer objects in the heap is different from that of strings, tuples or dictionaries, because integers mean different storage requirements and speed/space tradeoffs. Therefore, the Python memory manager delegates some work to the object-specific allocator, but ensures that the latter runs within the boundaries of the heap.

It is important to understand that Python heap management is performed by the interpreter itself, and users cannot control it, although they often manipulate object pointers to memory blocks in the heap. Python memory manager performs heap space allocation of Python objects and other internal buffers on demand through Python/C API functions listed in this document.

To avoid memory crash, extension writers should not try to manipulate Python objects with functions exported from C library: malloc (), calloc (), realloc () and free (). This will lead to the fatal consequences of the mixed call of C allocator and Python memory manager, because they implement different algorithms and run on different heaps.

In most cases, we recommend allocating memory from Python heap, because the latter is controlled by Python memory manager. For example, this is necessary when a new object type written in C extends the interpreter. Another reason to use the Python heap is to inform the Python memory manager about the memory requirements of the extension module. Entrusting all memory requests to Python memory manager will also give the interpreter a more accurate memory occupation as a whole. So in some cases, Python memory manager may or may not trigger appropriate operations, such as garbage collection, memory compression, and so on.