为什么禁用垃圾回收器? [英] Why disable the garbage collector?

查看:100
本文介绍了为什么禁用垃圾回收器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pythons gc.disable 禁用自动垃圾收集。据我了解,这会产生一些副作用。为什么有人想禁用自动垃圾回收,以及如何在没有它的情况下有效地管理内存?

Pythons gc.disable disables automatic garbage collection. As I understand it, that would have quite some side-effects. Why would anyone want to disable automatic garbage collection, and how could one effectively manage memory without it?

推荐答案

禁用垃圾收集器将在计算代码性能时获得更一致的结果。 timeit

One use for disabling the garbage collector is to get more consistent results when timing the performance of code. The timeit module does this.

def timeit(self, number=default_number):
    if itertools:
        it = itertools.repeat(None, number)
    else:
        it = [None] * number
    gcold = gc.isenabled()
    gc.disable()
    ...

在Python2中 Python3.2 gc.disable()也用于避免 fork 和 exec 。该问题似乎已在 Python3.3 中得到解决无需调用 gc.disable()

In Python2 and up to Python3.2 gc.disable() is also used to avoid a bug caused by garbage collection occurring between fork and exec. The problem seems to have been fixed in Python3.3 without needing to call gc.disable().

这篇关于为什么禁用垃圾回收器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆