2

python | 标记清除

 1 year ago
source link: https://benpaodewoniu.github.io/2022/12/29/python185/
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 标记清除。

循环引用 & 交叉感染

v1 = [11,22,33] # refchain 中创建一个列表对象,引用计数器 1
v2 = [44,55,66] # refchain 中创建一个列表对象,引用计数器 1

v1.append(v2)
v2.append(v1)

del v1
del v2

为了解决引用计数器循环引用的不足。

在 python 的底层再去维护一个链表,这个链表专门放那些可能存在循环引用的对象。

所以,此时,内存中有两个链表了。

python 在某些情况下,会扫描可能存在循环引用的链表,检查是否有循环引用。

如果有,则让双方的引用计数器 -1,如果引用计数器为 0 则进行垃圾回收。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK