在哪里调用 gc.collect()

dow*_*123 5 python garbage-collection memory-leaks memory-management

我应该在哪里调用垃圾收集器?调用函数后调用它会清除被调用函数的内存吗?

del另外,调用 gc.collect() 和专门对变量执行操作之间有什么区别

def a():
    b()
    # should I call gc.collect() here?
    # is there any other way to release memory allocated in the called function here?

def b():
    # big allocation like
    foo = ['abc' for x in range(10**7)]

    # should I call gc.collect() here?
    # should I do a foo = None and x = None here?
Run Code Online (Sandbox Code Playgroud)

Dan*_*man 2

应该调用它。

垃圾收集器仅用于清除引用计数机制未释放的循环引用。它不会解决您的内存不足问题;如果你有内存泄漏,你应该修复它。