How does Python's Garbage Collector Detect Circular References?

后端 未结 3 1651
后悔当初
后悔当初 2021-02-01 02:22

I\'m trying to understand how Python\'s garbage collector detects circular references. When I look at the documentation, all I see is a statement that circular references are de

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-01 02:57

    How does Python detect & free circular memory references before making use of the gc module?

    Python's garbage collector (not actually the gc module, which is just the Python interface to the garbage collector) does this. So, Python doesn't detect and free circular memory references before making use of the garbage collector.

    Python ordinarily frees most objects as soon as their reference count reaches zero. (I say "most" because it never frees, for example, small integers or interned strings.) In the case of circular references, this never happens, so the garbage collector periodically walks memory and frees circularly-referenced objects.

    This is all CPython-specific, of course. Other Python implementations have different memory management (Jython = Java VM, IronPython = Microsoft .NET CLR).

提交回复
热议问题