1

python | 缓存机制

 1 year ago
source link: https://benpaodewoniu.github.io/2022/12/29/python187/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

python | 缓存机制

2022-12-29python进阶内存管理

3

对引用计数器、标记清除、分代回收进行了优化,引出了缓存机制。

为了避免重复创建和销毁一些创建对象,弄了一个维护池。

v1 = 7
v2 = 9

python 启动解释器的时候,会创建 -5-4257 的值。上述的 v1v2 不会开辟内存,而是直接去池中获取。

如果此时 v3 = 9,实际上 v2v3 的内存地址是一样的。

所以,池中的值是不会被垃圾回收的。

free_list

当一个对象的引用计数器为 0,按理说应该回收,但是,内部不会被直接回收,而是将对象添加到 free_list 链表中当缓存。

以后再去创建对象,不会重新开辟内存,而是直接使用 free_list

每个类型的 free_list 有上限,比如

  • free_list
    • 最多 20 个 元组
      • 这个有点特殊,不详细说明了
    • 最多 80dict
    • 最多 80list
请我喝杯咖啡吧~

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK