Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当我反复打开和关闭同一个文件时,为什么内存使用量不断增长_Python_Linux - Fatal编程技术网

Python 当我反复打开和关闭同一个文件时,为什么内存使用量不断增长

Python 当我反复打开和关闭同一个文件时,为什么内存使用量不断增长,python,linux,Python,Linux,代码如下: while True: with open('a', 'w'): assert True # just do sth 当我运行它时,系统的总内存使用量一直在增长。 我的4核cpu都达到了100% 为什么会这样 Linux内核不应该立即关闭fds并释放内存吗 我的环境: Ubuntu 14.04内核:x86_64 Linux 3.13.0-77-generic Python 2.7.6 CPU:Intel Core i3-2350M CPU@2.3GHz 内

代码如下:

while True:
    with open('a', 'w'):
        assert True # just do sth
当我运行它时,系统的总内存使用量一直在增长。 我的4核cpu都达到了100%

为什么会这样

Linux内核不应该立即关闭fds并释放内存吗

我的环境:

  • Ubuntu 14.04内核:x86_64 Linux 3.13.0-77-generic
  • Python 2.7.6
  • CPU:Intel Core i3-2350M CPU@2.3GHz
  • 内存:7878MiB
现在我也不能重现这个问题了 即使运行原始代码:

def main():
    key_list = ""
    for i in xrange(10000000, 1000000000):
        key_list = key_list + str(i) + "\n"
        if len(key_list) > 10000:
            with open("./passwd1.txt", "a+") as f:
                f.write(key_list)
            # del key_list
            key_list = ""
            print "done"

奇怪…

我无法重现。哪种Python版本?您的所有4个CPU内核都通过一个非线程进程达到100%的利用率?似乎这里发生了其他事情。@arkascha
with
语句确保文件已关闭。Update@hek2mgl决定尝试运行更长时间。我无法在cpython 2.7或cpython 3.4中再现不断增加的内存使用。cpu使用率达到100%,但内存使用率保持不变。我想知道OP的RAM问题是否是由python运行时和linux文件系统或内核之外的其他原因引起的。某种监控过程,也许?也无法重现。还执行了导入gc检查垃圾收集器引用计数,但它保持不变。我无法复制。哪种Python版本?您的所有4个CPU内核都通过一个非线程进程达到100%的利用率?似乎这里发生了其他事情。@arkascha
with
语句确保文件已关闭。Update@hek2mgl决定尝试运行更长时间。我无法在cpython 2.7或cpython 3.4中再现不断增加的内存使用。cpu使用率达到100%,但内存使用率保持不变。我想知道OP的RAM问题是否是由python运行时和linux文件系统或内核之外的其他原因引起的。某种监控过程,也许?也无法重现。还执行了导入gc检查垃圾收集器引用计数,但它保持不变。