Why does python use both reference counting and mark-and-sweep for gc?

后端 未结 3 990
谎友^
谎友^ 2020-12-03 01:22

My question is why does python use both reference counting and mark-and-sweep for gc? Why not only mark-and-sweep?

My initial guess is that using reference counting

3条回答
  •  鱼传尺愫
    2020-12-03 01:54

    Python (the language) doesn't say which form of garbage collection it uses. The main implementation (often known as CPython) acts as you describe. Other versions such as Jython or IronPython use a purely garbage collected system.

    Yes, there is a benefit of earlier collection with reference counting, but the main reason CPython uses it is historical. Originally there was no garbage collection for cyclic objects so cycles led to memory leaks. The C APIs and data structures are based heavily around the principle of reference counting. When real garbage collection was added it wasn't an option to break the existing binary APIs and all the libraries that depended on them so the reference counting had to remain.

提交回复
热议问题