Manual garbage collection in Python

后端 未结 4 935
礼貌的吻别
礼貌的吻别 2020-12-09 17:34

Is there any way to manually remove an object which the garbage collection refuses to get rid of even when I call gc.collect()? Working in Python 3.0

4条回答
  •  执笔经年
    2020-12-09 17:57

    Per the docs, gc.get_referrers(thatobject) will tell you why the object is still alive (do it right after a gc.collect() to make sure the undesired "liveness" is gonna be persistent). After that, it's somehow of a black art;-). You'll often find that some of the referrers are lists (so WHY is that list referring to thatobject? you can .remove it in an emergency mode, but making the normal code sound is better...), and, even more often, dicts (many of whose may be __dict__s of some class instance or other -- often not trivial to find out which one... again, brute-force removal is sometimes an expedient emergency solution, but never a sustainable long-range one!-).

提交回复
热议问题