Large memory footprint of integers compared with result of sys.getsizeof()

后端 未结 2 479
面向向阳花
面向向阳花 2021-01-13 13:04

Python-Integer-objects in the range [1,2^30) need 28 byte, as provided by sys.getsizeof() and explained for example in this SO-post.

2条回答
  •  [愿得一人]
    2021-01-13 13:19

    The int object only requires 28 bytes, but Python uses 8-byte alignment: memory is allocated in blocks that are multiples of 8 bytes in size. So the actual memory used by each int object is 32 bytes. See this excellent article on Python memory management for more details.

    I don't yet have an explanation for the remaining half byte, but I'll update this if I find one.

提交回复
热议问题