我在python中编写了一个简单的脚本。这是一款有说服力的电池监听器,灵感来自IronMan Jarvis。现在的问题是,代码运行完美,但RAM在代码运行时不断变满。最后当RAM满了,PC崩溃了。可能是什么问题呢?我不使用任何新的变种。我通过轮询更新相同的变量。这是我的代码 -
# Get power status of the system using ctypes to call GetSystemPowerStatus
import ctypes
from ctypes import wintypes
import speech
def startmonitor():
class SYSTEM_POWER_STATUS(ctypes.Structure):
_fields_ = [
('ACLineStatus', wintypes.BYTE),
('BatteryFlag', wintypes.BYTE),
('BatteryLifePercent', wintypes.BYTE),
('Reserved1', wintypes.BYTE),
('BatteryLifeTime', wintypes.DWORD),
('BatteryFullLifeTime', wintypes.DWORD),
]
SYSTEM_POWER_STATUS_P = ctypes.POINTER(SYSTEM_POWER_STATUS)
GetSystemPowerStatus = ctypes.windll.kernel32.GetSystemPowerStatus
GetSystemPowerStatus.argtypes = [SYSTEM_POWER_STATUS_P]
GetSystemPowerStatus.restype = wintypes.BOOL
status = SYSTEM_POWER_STATUS() #define an object of the class SYSTEM_POWER_STATUS
if not GetSystemPowerStatus(ctypes.pointer(status)):
raise ctypes.WinError()
return status
x=0 #counting variable
def setflag0():
for x in range(7):
flag[x]=0
return flag
def setxval():
if(status.BatteryLifePercent==100):
x=0
elif(status.BatteryLifePercent<100 and status.BatteryLifePercent>30):
x=1
elif(status.BatteryLifePercent<=30 and status.BatteryLifePercent>15):
x=2
elif(status.BatteryLifePercent<=15 and status.BatteryLifePercent>5):
x=3
elif(status.BatteryLifePercent<=5):
x=4
return x
flag=[0,0,0,0,0,0,0]
speech.say("This is the talking battery monitor version 1.0")
while(1) :
status=startmonitor()
bat=setxval()
if(bat!=0 and status.ACLineStatus==1 and flag[1]!=1):
speech.say("All power systems being charged sir ! ")
flag=setflag0()
flag[1]=1
elif(bat==0 and flag[6]!=1):
if(status.ACLineStatus==1):
speech.say("Battery : one hundred percent charged")
elif(status.ACLineStatus==0):
speech.say("Sir , You have full battery power")
flag=setflag0()
flag[6]=1
elif(bat==1 and status.ACLineStatus==0 and flag[2]!=1):
speech.say("Battery Level : ")
speech.say(status.BatteryLifePercent)
speech.say("percent")
flag=setflag0()
flag[2]=1
elif(bat==2 and flag[3]!=1 and status.ACLineStatus==0):
speech.say("Sir, i'm running low on battery . Please put me on charge !" )
flag=setflag0()
flag[3]=1
elif(bat==3 and flag[4]!=1 and status.ACLineStatus==0):
speech.say("Sir, you're now running on emergency backup power")
flag=setflag0()
flag[4]=1
elif(bat==4 and flag[5]!=1 and status.ACLineStatus==0):
speech.say(" Alert ! Power critical Sir, I might turn off in ")
speech.say(status.BatteryLifeTime/1000000)
speech.say("minutes")
flag=setflag0()
flag[5]=1
答案 0 :(得分:0)
循环中的某个地方,一个函数正在使用内存。
可能是系统调用SYSTEM_POWER_STATUS正在使用内存。 是否可以致电
status=startmonitor()
在while循环之前?
否则开始删除代码(即使它暂时破坏了功能)以找出哪个部分使用内存并尝试为该部分找到替代方案。