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

后端 未结 3 993
谎友^
谎友^ 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 02:07

    My initial guess is that using reference counting can easily remove non-cyclic referenced objects, this may somewhat speed up mark-and-sweep and gain memory immediately. Don't know if my guess is right?

    Yes. As soon as the refcount goes to zero and object can be removed. This won't happen in a cyclic referenced object. AFAIK, mark and sweep is a costly operation and the simplest way to implement it requires you to "stop the world" while objects are marked. When all of the objects are traversed, andy object not marked (as reachable) is released.

提交回复
热议问题