python需要手动释放内存吗_Python没有释放内存

这篇博客探讨了在使用Python的lxml和xml.etree.ElementTree库处理XML资源时遇到的内存泄漏问题。即使调用了垃圾回收器gc.collect(),内存仍然被占用。作者发现,通过调用len(gc.get_objects())可以释放内存,保持进程资源的正常状态。这个问题在Ubuntu 13.04和Python 2.7.4环境下出现。
摘要由CSDN通过智能技术生成

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.

Imports:

import time

from lxml import etree

import gc

This is the code:

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()

And this is my main

if __name__ == "__main__":

while True:

print "Wake up!"

process_alternative()

print "Sleeping..."

time.sleep(30)

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?

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)]

解决方案

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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>