sleep导致内存泄漏

sleep导致内存泄漏

本文介绍了Python-为什么time.sleep导致内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行下面的代码时,内存正在增加.但是,如果我删除了time.sleep(3),它在top中是0.1,并且从没有增加.

When I ran the code below, the memory was increasing. However if I deleted time.sleep(3), it was 0.1 in top and never increased.

process似乎没有正确终止,但是为什么?

It seems process not be terminated correctly, but why?

代码(Python 2.7.11):

import time
import multiprocessing

def process():
    #: FIXME
    time.sleep(3)
    return

def main():
    pool = multiprocessing.Pool(processes=10)
    while 1:
        pool.apply_async(process)
    pool.close()
    pool.join()

if __name__ == '__main__':
    main()

推荐答案

据我所知,我认为当您在同一个Pool中生成新进程时,垃圾回收永远不会完成,因此您不会释放内存即使您已完成使用旧流程的操作,也可以使用它们.一种解决方法是在while循环中强制执行垃圾回收:

As far as I know, I think that as you spawn new processes in the same Pool, the garbage collection is never done, so you don't release memory from old processes, even if you are done using them.One fix would be to enforce the garbage collection in your while loop:

import time
import multiprocessing
import gc

def process():
    time.sleep(3)
    return

def main():
    pool = multiprocessing.Pool(processes=10)
    while 1:
        pool.apply_async(process)
        gc.collect()
    pool.close()
    pool.join()


if __name__ == '__main__':
    main()

这为我解决了内存泄漏,因为您在启动另一组进程之前强制执行垃圾回收.我希望有人可以更详细地解释出现此内存泄漏的原因.

This fixed the memory leaks for me, as you force the garbage collection before launching another set of processes.I hope someone could explain in a more detailed way the reason behing this memory leak.

这篇关于Python-为什么time.sleep导致内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:39