my RAM gets full and PC crashes when i execute my python script .
Question: I wrote a simple script in python . It's a talking battery monitor , inspired
from IronMan Jarvis . Now the problem is that , the code runs perfectly but
the RAM keeps getting full when the code is running . Finally when the RAM
gets full , the PC crashes . What could be the problem? I dont use any new
varibles . I update the same variable by polling . This is my code-

    
    
    # 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
    


Answer: Somewhere in the loop a function is using memory.

It might be that the system call SYSTEM_POWER_STATUS is using memory. Is it
possible to call

    
    
    status=startmonitor() 
    

before the while loop?

Otherwise start removing code (even though it breaks the functionality
temporarily) to find out which part uses memory and try to find an alternative
for that part.

