yingjil 2023-05-11 16:15 采纳率: 50%
浏览 12

python的None对象为什么是16字节

python的None对象为什么是16字节?

In [64]: sys.getsizeof(None)
Out[64]: 16

Bing说:

The size of the None object in Python is 16 bytes 1. This is because Python uses a fixed-size object allocation mechanism for small objects like None 2. The size of the memory allocated is determined by the size of the object. For example, the size of the None object is 16 bytes, so Python pre-allocates 16 bytes of memory for each None object 2. This mechanism is used to improve performance by reducing the overhead associated with allocating and deallocating memory for small objects 3.

但感觉没有说透。https://github.com/python/cpython/blob/4abfe6a14b5be5decbaa3142d9e2549cf2d86c34/Doc/c-api/none.rst#L3 也没有直观的答案。似乎只有一个PyObject指针。

Python pre-allocates 16 bytes of memory for each None object 在哪里控制的?

  • 写回答

5条回答 默认 最新

  • threenewbee 2023-05-11 16:19
    关注

    这说的很清楚,就是出于性能考虑,用空间换时间,因为频繁分配大量小内存空间会造成更多碎片,也会更费时间

    评论

报告相同问题?

问题事件

  • 创建了问题 5月11日

悬赏问题

  • ¥15 来个厉害的程序开发的,安排点活给你做
  • ¥15 rexroth indramotion MTX micro系统轴耦合编程
  • ¥100 springboot2.7.x 整合 sharding 的问题
  • ¥15 如何通过命令行操作统信360安全浏览器?
  • ¥15 upload-labs-master第三关
  • ¥15 关于LT3758反激式负压电源,功率三极管烧毁的问题
  • ¥20 aruba ap305 CAP转IAP
  • ¥20 rockchip rk3588 ffmedia运行异常
  • ¥30 如何用 Java 简单系统设计?(相关搜索:服务器|面向对象设计|随机数)
  • ¥15 错误使用 mex Embedding metadata in the MEX file failed.