Manual garbage collection in Python

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

    It depends on what your Python is running on. Here's good article that explains the details

    Quoting:

    In current releases of CPython, each new assignment to x inside the loop will release the previously allocated resource. Using GC, this is not guaranteed. If you want to write code that will work with any Python implementation, you should explicitly close the resource; this will work regardless of GC:

    for name in big_list:
        x = Resource()
        do something with x
        x.close()
    

提交回复
热议问题