1
python | 缓存机制
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.
python | 缓存机制
对引用计数器、标记清除、分代回收进行了优化,引出了缓存机制。
为了避免重复创建和销毁一些创建对象,弄了一个维护池。
v1 = 7
v2 = 9
python 启动解释器的时候,会创建 -5
、-4
… 257
的值。上述的 v1
和 v2
不会开辟内存,而是直接去池中获取。
如果此时 v3 = 9
,实际上 v2
和 v3
的内存地址是一样的。
所以,池中的值是不会被垃圾回收的。
free_list
当一个对象的引用计数器为 0
,按理说应该回收,但是,内部不会被直接回收,而是将对象添加到 free_list
链表中当缓存。
以后再去创建对象,不会重新开辟内存,而是直接使用 free_list
。
每个类型的 free_list 有上限,比如
free_list
- 最多
20
个 元组- 这个有点特殊,不详细说明了
- 最多
80
个dict
- 最多
80
个list
- 最多
请我喝杯咖啡吧~
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK