0

我用 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
4

1 回答 1

0

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

可能是系统调用 SYSTEM_POWER_STATUS 正在使用内存。是否可以打电话

status=startmonitor() 

在while循环之前?

否则开始删除代码(即使它暂时破坏了功能)以找出哪个部分使用内存并尝试为该部分找到替代方案。

于 2013-10-04T07:58:54.567 回答