Python字典和集合内存分配

标签 python python-3.x memory memory-management

我找到了this excellent resource讨论 python 字典和集合的内存使用情况,具体如下:

By default, the smallest size of a dictionary or set is 8 (that is, if you are only storying 3 values, python will still allocate 8 elements). On resize, the number of buckets increases by 4x until we reach 50,000 elements, after which the size is increased by 2x. This gives the following possible sizes,

16, 64, 256, 1024, 4096, 16384, 65536, 131072, 262144, ...

It is important to note that resizing can happen to make a hash table larger OR smaller. That is, if sufficiently many elements of a hash table are deleted, the table can be scaled down in size. This is because the consideration for the table being 2/3rds full uses the total of inserted and deleted entries since the last resize. However, resizing only happens during an insert.

但这篇文章发表于 2014 年 9 月,因此很可能是在此之前的某个时间写的。这在最新版本的 Python 中仍然准确且相关吗? (3.6+)

最佳答案

在 CPython 中,情况不再如此。自从那里写的内容以来,字典实现的这个特定部分已经改变了几次。评论始于 this line GROWTH_FACTOR 定义的正上方给出了一些历史记录。

/* GROWTH_RATE. Growth rate upon hitting maximum load.
 * Currently set to used*3.
 * This means that dicts double in size when growing without deletions,
 * but have more head room when the number of deletions is on a par with the
 * number of insertions.  See also bpo-17563 and bpo-33205.
 *
 * GROWTH_RATE was set to used*4 up to version 3.2.
 * GROWTH_RATE was set to used*2 in version 3.3.0
 * GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0.
 */

书中提供的信息在出版时已过时大约两年。

关于Python字典和集合内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52377489/

相关文章:

objective-c - 在 applicationWillResignActive 中发送内存警告

c++ - 函数的 C/C++ 内存管理

c++ - 使用动态数组的简单地址簿

python - PySpark 将数据帧数组除以 float

python - 自定义和非托管用户模型/滞后用户表

python - NumPy 错误 : The truth value of an array with more than one element is ambiguous. 使用 a.any() 或 a.all()

python - Django 中未加载 CSS 静态文件

python-3.x - pkg_resources.resource_filename 未提取文件

python - 如何用 Python 实现 Comet 服务器端?

python - 无法在 python3.1 中以功能方式关闭文件?