def foo(): exc_type,exc_value,exc_tb = sys.exc_info() try: # some code except: # some code finally: del exc_type,exc_tb解决方法 是的,至少对于exc_tb; traceback对象包含对当前帧的引用,这使得它成为循环引用.
通过删除本地引用,您可以打破该圈,因此您不必希望并相信垃圾收集器能够.
从sys.exc_info()
function docs:
总结Warning: Assigning the traceback return value to a local variable in a function that is handling an exception will cause a circular reference. This will prevent anything referenced by a local variable in the same function or by the traceback from being garbage collected. Since most functions don’t need access to the traceback,the best solution is to use something like
exctype,value = sys.exc_info()[:2]
to extract only the exception type and value. If you do need the traceback,make sure to delete it after use (best done with atry
…finally
statement) or to callexc_info()
in a function that does not itself handle an exception.
以上是内存溢出为你收集整理的删除Python标准库中的变量全部内容,希望文章能够帮你解决删除Python标准库中的变量所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)