垃圾收集器会跟踪所有Python对象吗? [英] Are all Python objects tracked by the garbage collector?

查看:78
本文介绍了垃圾收集器会跟踪所有Python对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调试内存泄漏(请参阅问题内存在Twisted中泄漏:它在哪里?).

I'm trying to debug a memory leak (see question Memory leak in Python Twisted: where is it?).

运行垃圾收集器时,它可以访问由Python解释器创建的所有Python对象吗?如果我们假设Python C库没有泄漏,那么RSS内存使用量是否应该相对于GC对象数呈线性增长? sys.getobjects呢?

When the garbage collector is running, does it have access to all Python objects created by the Python interpreter? If we suppose Python C libraries are not leaking, should RSS memory usage grow linearly with respect to the GC object count? What about sys.getobjects?

推荐答案

CPython使用两种机制来清理垃圾.一种是引用计数,它影响所有对象,但不能清除(直接或间接)相互引用的对象.这就是实际的垃圾收集器的来源:python有gc模块,该模块在它知道的对象中搜索循环引用.只有可能成为参考周期一部分的对象才需要担心是否要参与循环gc.因此,例如,列表可以,但字符串不可以;字符串不引用任何其他对象. (实际上,这个故事有点复杂,因为有两种参与循环gc的方式,但这在这里并不重要.)

CPython uses two mechanisms to clean up garbage. One is reference counting, which affects all objects but which can't clean up objects that (directly or indirectly) refer to each other. That's where the actual garbage collector comes in: python has the gc module, which searches for cyclic references in objects it knows about. Only objects that can potentially be part of a reference cycle need to worry about participating in the cyclic gc. So, for example, lists do, but strings do not; strings don't reference any other objects. (In fact, the story is a little more complicated, as there's two ways of participating in cyclic gc, but that isn't really relevant here.)

循环gc自动跟踪所有Python类(及其实例).除非稍加努力,否则用C定义的类型不是.所有可能属于循环的内建类型都可以.但这确实意味着gc模块仅了解所伴随的类型.

All Python classes (and instances thereof) automatically get tracked by the cyclic gc. Types defined in C aren't, unless they put in a little effort. All the builtin types that could be part of a cycle do. But this does mean the gc module only knows about the types that play along.

除收集机制外,Python还具有自己的聚合内存分配器(obmalloc),该分配器分配整个内存区域,并将内存用于其创建的大多数较小对象.现在,Python会在这些竞技场完全变空时(很长一段时间没有)释放这些竞技场,但是实际上清空竞技场非常罕见:因为CPython对象是不可移动的,所以不能只将一些散乱的对象移到另一个竞技场.

Apart from the collection mechanism there's also the fact that Python has its own aggregating memory allocator (obmalloc), which allocates entire memory arenas and uses the memory for most of the smaller objects it creates. Python now does free these arenas when they're completely empty (for a long time it didn't), but actually emptying an arena is fairly rare: because CPython objects aren't movable, you can't just move some stragglers to another arena.

这篇关于垃圾收集器会跟踪所有Python对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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