Python没有释放内存 [英] Python is not freeing memory

查看:476
本文介绍了Python没有释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用XML资源,而且看来Python正在发布一种奇怪的行为.我已经测试了lxml库和xml.etree.ElementTree,它们都应在gc收集之后保存内存.我输入gc.collect()作为测试,但没有其他反应:内存仍由进程保留.

I've been working with XML resources, and it seems that Python is issuing a weird behavior. I've tested both lxml library and xml.etree.ElementTree, both holding memory after it should be collected by gc. I typed gc.collect() as a test, but nothing else happen: memory still being held by process.

进口:

import time
from lxml import etree
import gc

这是代码:

def process_alternative():
    """
    This alternative process will use lxml
    """
    filename = u"/tmp/randomness.xml"
    fd = open(filename, 'r')
    tree = etree.parse(fd)
    root = tree.getroot()

    accum = {}

    for _item in root.iter("*"):
        for _field in _item.iter("*"):
            if _field.tag in accum.keys():
                accum[_field.tag] += 1
            else:
                accum[_field.tag] = 1

    for key in accum.keys():
        print "%s -> %i" % (key, accum[key])

    fd.close()
    gc.collect()

这是我的主要爱好

if __name__ == "__main__":
    while True:
        print "Wake up!"
        process_alternative()
        print "Sleeping..."
        time.sleep(30)

如您所见,此主程序调用"process_alternative",然后进入睡眠状态.提供的XML文件加载了将近800MB的内存;因此,在time.sleep之前,应按进程释放内存,并返回所需的基本VM内存(大约32MB?).取而代之的是,进程继续保持在800MB左右.

As you see, this main calls "process_alternative", and then sleep. XML file provided loads memory with nearly 800MB; so, before time.sleep, memory should be freed by process, returning to basic VM memory needed (around 32MB?). Instead, process continue holding around 800MB.

关于为什么每次迭代后都没有释放内存的任何提示吗?

Any tip about why memory has not been freed after every iteration?

使用ubuntu 13.04,Python 2.7.4

Using ubuntu 13.04, Python 2.7.4

此函数在每次迭代中都会释放内存

This function deallocates memory in every iteration

def check_memory():
    ac1 = [a1**5 for a1 in xrange(10000000)]
    time.sleep(5)
    ac2 = [a1**5 for a1 in xrange(10000000)]
    time.sleep(5)
    ac3 = [a1**5 for a1 in xrange(10000000)]

推荐答案

我不知道为什么,但是即使我对gc.collect()设置了显式调用,进程仍然保留内存.

I do not know why, but process still hold memory, even when I set an explicit call to gc.collect().

经过一番比赛,多亏了马丁·彼得(Martijn Pieters),一个解决方案出现了.呼叫

After some playing, and thanks to Martijn Pieters, a solution appeared. Calling

len(gc.get_objects())

释放所有访问的内存,并在不忙时使进程继续使用正确的资源.奇怪,但事实如此.

frees all accessed memory, and keeps process on right resources when it's not busy. Strange, but true.

这篇关于Python没有释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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