我是一种新的python,我需要整天运行脚本。然而,脚本使用的内存不断增加,直到蟒蛇崩溃...我试过的东西,但没有任何工作:(也许我做错了什么,我不知道。这是我的代码是这样的:如何在python中释放内存?
while True:
try:
import functions and modules...
Init_Variables = ...
def a bunch of functions...
def clearall(): #Function to free memory
all = [var for var in globals() if var[0] != "_" and var != gc]
for var in all:
del globals()[var]
print('Cleared !')
gc.collect()
TimeLoop() #Getting out of "try" here because TimeLoop isn't defined anymore
def TimeLoop():
for i in range (1,101):
do stuff...
# Free memory after 100 iterations #############################
clearall()
TimeLoop() # Launches my function when i first start the script
except Exception as e:
print(e)
continue
break
经过约50.000迭代“做的东西......” Python使用约2GB的RAM,然后崩溃:( 我花了几个小时试图解决这个问题,但似乎没有任何工作。任何帮助将是非常赞赏!:D
很有可能不是连续运行相同的脚本,而是最好设置一个cronjob来间隔运行脚本。 – JJJ
就像运行X分钟的脚本,然后杀死并重新启动它?这种残酷但应该工作^^'。但是我想找到一个更柔和的工作哈哈:) –
编号运行脚本,让它完成,然后重新运行一分钟后。 – JJJ