Manual garbage collection in Python

后端 未结 4 934
礼貌的吻别
礼貌的吻别 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:59

    If the GC is refusing to destroy it, it's because you have a reference to it somewhere. Get rid of the reference and it will (eventually) go. For example:

    myRef = None
    

    Keep in mind that GC may not necessarily destroy your object unless it needs to.

    If your object is holding resources not under the management of Python (e.g., some trickery with C code called from Python), the object should provide a resource release call so you can do it when you want rather than when Python decides.

提交回复
热议问题