垃圾收集器和gc模块 [英] Garbage Collector and gc module

查看:120
本文介绍了垃圾收集器和gc模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


由于收藏家对已经使用的引用计数进行了补充,因此我在阅读文档时遇到了以下短语: Python,如果你确定你的程序没有创建引用周期,你可以禁用收集器。


这是什么意思?如果我禁用了垃圾回收器( gc.disable()),我会这样做:

  a ='hi'
a ='hello'

'hi'保留在内存中?我是否需要自己解放记忆?



我从这句话中明白的是,gc是一个额外的工具,特别是捕捉参考周期,如果它是禁用内存仍然使用对象的引用计数器自动清除,但引用循环将不受管理。

解决方案

在CPython中,当引用计数降至0时,对象会立即从内存中清除。



当您将 a 重新设为'hello'时,引用计数为'hi'字符串对象递减。如果它达到0,它将从内存中移除。



因此,垃圾收集器只需处理(间接或直接)引用对象,从而保持引用计数不会降到0。



字符串不能引用其他对象,所以对垃圾收集器不感兴趣。但是任何可以引用其他内容的东西(比如列表或字典的容器类型,或任何Python类或实例)都可以产生循环引用:

  a = []#Ref count是1 
a.append(a)#循环引用! Ref count现在为2
del a#Ref count递减为1

垃圾回收器检测这些循环引用;没有其他引用 a ,所以最终gc进程打破了圆圈,让引用计数自然降到0。 >顺带提一句,Python编译器将字符串文字如'hi''hello'作为常量与生成的字节码进行比较,因此总是至少有一个这样的对象的引用。另外,与正则表达式 [a-zA-Z0-9 _] 匹配的源代码中使用的字符串文字是 interned ;作为单例来减少内存占用,因此其他使用相同字符串文字的代码块将保存对同一共享字符串的引用。


I was reading the documentation when I came in doubt with the following phrase:

Since the collector supplements the reference counting already used in Python, you can disable the collector if you are sure your program does not create reference cycles.

What does this mean? If I disable the garbage collector (gc.disable()) and I do something like this:

a = 'hi'
a = 'hello'

will 'hi' remain in memory? Do I need to free the memory by myself?

What I understood from that sentence is that the gc is an extra tool made up expecially to catch reference cycles and if it is disabled the memory is still automatically cleaned using the reference counters of the objects but the reference cycles will not be managed. Is that right?

解决方案

In CPython, objects are cleared from memory immediately when their reference count drops to 0.

The moment you rebind a to 'hello', the reference count for the 'hi' string object is decremented. If it reaches 0, it'll be removed from memory.

As such, the garbage collector only needs to deal with objects that (indirectly or directly) reference one another, and thus keep the reference count from ever dropping to 0.

Strings cannot reference other objects, so are not of interest to the garbage collector. But anything that can reference something else (such as containers types such as lists or dictionaries, or any Python class or instance) can produce a circular reference:

a = []  # Ref count is 1
a.append(a)  # A circular reference! Ref count is now 2
del a   # Ref count is decremented to 1

The garbage collector detects these circular references; nothing else references a, so eventually the gc process breaks the circle, letting the reference counts drop to 0 naturally.

Incidentally, the Python compiler bundles string literals such as 'hi' and 'hello' as constants with the bytecode produced and as such, there is always at least one reference to such objects. In addition, string literals used in source code that match the regular expression [a-zA-Z0-9_] are interned; made into singletons to reduce the memory footprint, so other code blocks that use the same string literal will hold a reference to the same shared string.

这篇关于垃圾收集器和gc模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆