当我执行python脚本时,我的RAM满了,PC崩溃了。

当我执行python脚本时,我的RAM满了,PC崩溃了。,python,memory,crash,overflow,ram,Python,Memory,Crash,Overflow,Ram,我用python编写了一个简单的脚本。这是一款会说话的电池显示器,灵感来自铁人贾维斯。现在的问题是,代码运行得很好,但当代码运行时,RAM会不断变满。最后,当RAM满时,PC崩溃。有什么问题吗?我不使用任何新品种。我通过轮询更新相同的变量。这是我的密码- # Get power status of the system using ctypes to call GetSystemPowerStatus import ctypes from ctypes import wintypes impo

我用python编写了一个简单的脚本。这是一款会说话的电池显示器,灵感来自铁人贾维斯。现在的问题是,代码运行得很好,但当代码运行时,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
#使用ctypes调用GetSystemPowerStatus获取系统的电源状态
导入ctypes
从ctypes导入wintypes
重要讲话
def startmonitor():
类别系统\u电源\u状态(C类型结构):
_字段=[
('ACLineStatus',wintypes.BYTE),
('BatteryFlag',wintypes.BYTE),
('BatteryLifePercent',wintypes.BYTE),
('Reserved1',wintypes.BYTE),
('BatteryLifeTime',wintypes.DWORD),
('BatteryFullLifeTime',wintypes.DWORD),
]
SYSTEM\u POWER\u STATUS\u P=C类型指针(SYSTEM\u POWER\u STATUS)
GetSystemPowerStatus=ctypes.windell.kernel32.GetSystemPowerStatus
GetSystemPowerStatus.argtypes=[SYSTEM\u POWER\u STATUS\u P]
GetSystemPowerStatus.restype=wintypes.BOOL
status=SYSTEM_POWER_status()#定义类SYSTEM_POWER_status的对象
如果不是GetSystemPowerStatus(ctypes.pointer(状态)):
提升ctypes.WinError()
返回状态
x=0#计数变量
def setflag0():
对于范围(7)内的x:
标志[x]=0
返回标志
def setxval():
如果(status.BatteryLifePercent==100):
x=0
elif(状态:电池寿命百分比30):
x=1
elif(状态:电池寿命百分比15):
x=2
elif(状态:电池寿命百分比5):
x=3

elif(status.BatteryLifePercent循环中某个函数正在使用内存

可能是系统调用system_POWER_状态正在使用内存。 可以打电话吗

status=startmonitor() 
在while循环之前

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