2013-10-04 23 views
-1

我在python中编写了一个简单的脚本。这是一个谈话电池监视器,从IronMan贾维斯的启发。现在的问题是,代码运行完美,但代码运行时内存不断充满。最后,当RAM变满时,PC崩溃。可能是什么问题呢?我不使用任何新的变数。我通过轮询来更新相同的变量。这是我的代码 -当我执行我的python脚本时,我的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

循环中的某个地方某个函数正在使用内存。

可能是系统调用SYSTEM_POWER_STATUS正在使用内存。 可以呼叫

status=startmonitor() 

while while循环之前吗?

否则开始删除代码(即使它暂时中断功能)以找出哪个部分使用内存并尝试找到该部分的替代方法。

相关问题