Python does a lot of allocations and deallocations. All objects,
including "simple" types like integers and floats, are stored on the
heap. Calling malloc and free for each variable would be very slow.
Hence, the Python interpreter uses a variety of optimized memory
allocation schemes. The most important one is a malloc implementation
called pymalloc, designed specifically to handle large numbers of
small allocations. Any object that is smaller than 256 bytes uses this
allocator, while anything larger uses the system's malloc.Memory allocation works at several levels in Python. There’s the
system’s own allocator, which is what shows up when you check the
memory use using the Windows Task Manager or ps. Then there’s the C
runtime’s memory allocator (malloc), which gets memory from the system
allocator, and hands it out in smaller chunks to the application.
Finally, there’s Python’s own object allocator, which is used for
objects up to 256 bytes. This allocator grabs large chunks of memory
from the C allocator, and chops them up in smaller pieces using an
algorithm carefully tuned for Python.
特别是浮点数:floats also use an immortal & unbounded free list.
所以不,只有在pythons free list中没有更多可用的float点时才会分配它,这取决于程序中以前的float使用情况。在
归根结底,python正在为您进行内存管理,因此即使您的问题得到了可靠的回答,也不会给您带来太多的洞察力。在