gpt4 book ai didi

python - 为什么这个Python函数执行后不释放内存?

转载 作者:行者123 更新时间:2023-11-28 19:19:22 31 4
gpt4 key购买 nike

我有一个 Python 脚本,它通过 Beautiful Soup 传递的 XML 文件查找文本术语。然后我遍历到我想要提取的值并返回字符串。这行得通,但是因为我在程序主循环中添加了对该函数的调用,所以我最终遇到了 MemoryError 问题。查看 Windows 任务管理器,我发现每次调用后内存都没有释放,系统内存不足。问题似乎是“return myLink.string”行。这是否包含对内存中对象的引用,我该如何解决这个问题?

def FindUptime(soup):
link = soup.find(text=re.compile("Uptime"))
if link:
myLink = link.parent
myLink = myLink.next_sibling
if myLink.string == "Up:":
myLink = myLink.next_sibling
myLink = myLink.next_element
else:
print "Can't find Up: term"
return myLink.string
else:
print "Can't find Uptime"
return ""

调用者

readings[sensorIds[i][SENSOR_NAME]] = FindUptime(soup)

最佳答案

修改为...

def FindUptime(soup):
link = soup.find(text=re.compile("Uptime"))
if link:
myLink = link.parent
myLink = myLink.next_sibling
if myLink.string == "Up:":
myLink = myLink.next_sibling
myLink = myLink.next_element
else:
print "Can't find Up: term"
uptime = str(myLink.string) # added
return uptime
else:
print "Can't find Uptime"
return ""

关于python - 为什么这个Python函数执行后不释放内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28962338/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com