内存地址 Memory Management

Memory Management

https://docs.python.org/2/c-api/memory.html

Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager. The Python memory manager has different components which deal with various dynamic storage management aspects, like sharing, segmentation, preallocation or caching.

At the lowest level, a raw memory allocator ensures that there is enough room in the private heap for storing all Python-related data by interacting with the memory manager of the operating system. On top of the raw memory allocator, several object-specific allocators operate on the same heap and implement distinct memory management policies adapted to the peculiarities of every object type. For example, integer objects are managed differently within the heap than strings, tuples or dictionaries because integers imply different storage requirements and speed/space tradeoffs. The Python memory manager thus delegates some of the work to the object-specific allocators, but ensures that the latter operate within the bounds of the private heap.

It is important to understand that the management of the Python heap is performed by the interpreter itself and that the user has no control over it, even if she regularly manipulates object pointers to memory blocks inside that heap. The allocation of heap space for Python objects and other internal buffers is performed on demand by the Python memory manager through the Python/C API functions listed in this document.

To avoid memory corruption, extension writers should never try to operate on Python objects with the functions exported by the C library: malloc(), calloc(), realloc() and free(). This will result in mixed calls between the C allocator and the Python memory manager with fatal consequences, because they implement different algorithms and operate on different heaps. However, one may safely allocate and release memory blocks with the C library allocator for individual purposes, as shown in the following example:

w

import ctypes

w = 'w_python_c'
print ctypes.cast(id(w), ctypes.py_object).value

print id(w)
print id(w)
wuser@ubuntu:~/apiamzpy$ python wrf.py
w_python_c
<xml.etree.ElementTree._IterParseIterator object at 0x7fd33214d1d0>
('end', <Element '{https://mws.amazonservices.com/Orders/2013-09-01}AmazonOrderId' at 0x7fd332167910>)
('end', <Element '{https://mws.amazonservices.com/Orders/2013-09-01}ASIN' at 0x7fd332167a50>)
('end', <Element '{https://mws.amazonservices.com/Orders/2013-09-01}SellerSKU' at 0x7fd332167a90>)
('end', <Element '{https://mws.amazonservices.com/Orders/2013-09-01}OrderItemId' at 0x7fd332167ad0>)
('end', <Element '{https://mws.amazonservices.com/Orders/2013-09-01}Title' at 0x7fd332167b10>)
('end', <Element '{https://mws.amazonservices.com/Orders/2013-09-01}QuantityOrdered' at 0x7fd332167b50>)
('end', <Element '{https://mws.amazonservices.com/Orders/2013-09-01}QuantityShipped' at 0x7fd332167b90>)
('end', <Element '{https://mws.amazonservices.com/Orders/2013-09-01}PromotionIds' at 0x7fd332167bd0>)
('end', <Element '{https://mws.amazonservices.com/Orders/2013-09-01}OrderItem' at 0x7fd332167a10>)
('end', <Element '{https://mws.amazonservices.com/Orders/2013-09-01}OrderItems' at 0x7fd332167990>)
('end', <Element '{https://mws.amazonservices.com/Orders/2013-09-01}ListOrderItemsResult' at 0x7fd3321678d0>)
('end', <Element '{https://mws.amazonservices.com/Orders/2013-09-01}RequestId' at 0x7fd332167c50>)
('end', <Element '{https://mws.amazonservices.com/Orders/2013-09-01}ResponseMetadata' at 0x7fd332167c10>)
('end', <Element '{https://mws.amazonservices.com/Orders/2013-09-01}ListOrderItemsResponse' at 0x7fd332167890>)
原文地址:https://www.cnblogs.com/rsapaper/p/6802748.html