title: Tkinter OpenCV Python - Fail to allocate memory after 2h
body: <p>I have created a simple video game BOT using openCV 4.6.0 and 64-bit python.
  Program flow listed below;</p>
  <pre><code>1. Open GUI using tkinker 
  2. Upon pressing start a new process opens in a loop (main loop)
  3. Program takes a screenshot of my screen (850x650 pixels) and saves as PNG
  4. Load in the screenshot to openCV MAT and start image processing with cv.matchtemplate
  5. Repeat main loop. After 1 hour kill this process, open a new process and repeat
  </code></pre>
  <p>The reason why I have this running on a separate process was because I thought that maybe openCV was locking memory in the background so I'm essentially doing a &quot;Reboot&quot;.</p>
  <p>All the OpenCV related code is below;</p>
  <pre><code> #Static Images
   hp_img = cv.imread(LOCAL_DIRECTORY + 'hp.png', cv.COLOR_BGR2GRAY)
   worldmap_img = cv.imread(LOCAL_DIRECTORY + 'worldmap.png', cv.COLOR_BGR2GRAY)
   settings_menu = cv.imread(LOCAL_DIRECTORY + 'settings.png', cv.COLOR_BGR2GRAY)
   black_screen = cv.imread(LOCAL_DIRECTORY + 'blackscreen.png', cv.COLOR_BGR2GRAY)
  
  def checkBlackScreen():
  global threshold
  
  screenShot = pyautogui.screenshot(LOCAL_DIRECTORY + &quot;screen.png&quot;, region=(0, 0, 850, 650))
  
  screen_img = cv.imread(LOCAL_DIRECTORY + 'screen.png', cv.COLOR_BGR2GRAY)
  result = cv.matchTemplate(screen_img, black_screen, cv.TM_CCOEFF_NORMED)
  _, max_val, _, max_loc = cv.minMaxLoc(result)
  
  del result
  del screen_img
  del screenShot
  gc.collect()
  
  if (max_val &gt; threshold):
      print(&quot;[BAD] Stuck in black screen&quot;)
      return True
  else:
      print(&quot;[Good] Not stuck in black screen : &quot;, max_val)
      return False
  </code></pre>
  <p>The main loop just calls the function above and presses buttons accordingly. Most of the program provided below;</p>
  <pre><code>#Main entry of the program    
  if __name__ == '__main__':    
      GUI()  
  
  #GUI using TK
  def GUI():
  
      root = Tk()
      frm = ttk.Frame(root, padding=10)
      frm.grid()
      ttk.Label(frm, text=&quot;Hello World!&quot;).grid(column=0, row=0)
      ttk.Button(frm, text=&quot;Quit&quot;, command=root.destroy).grid(column=1, row=0)
      ttk.Button(frm, text=&quot;Start&quot;, command=startBotProcess).grid(column =2, row=0)
      ttk.Button(frm, text=&quot;Stop&quot;, command=stopBot).grid(column =3, row=0)
      ttk.Button(frm, text=&quot;Set WorldMap XY&quot;, command=StartListenClick).grid(column =4, row=0)
      #ttk.Button(frm, text=&quot;Set WorldMap XY&quot;, command=checkEXPMap).grid(column =4, row=0)
      root.mainloop()
  
  #Set world map X,Y    
  def on_click(x, y, button, pressed):
      listen_for_mouse = False
      if pressed:
          global MOUSE_X
          global MOUSE_Y
          
          MOUSE_X = x
          MOUSE_Y = y
      
          print('World Map set to : ',(MOUSE_X, MOUSE_Y))
          listen_for_mouse = False
      if not pressed:
          # Stop listener
          return False    
  
  # Collect events until released    
  def StartListenClick():
      listen_for_mouse = True
      with Listener(on_click=on_click) as listener:
          if (listen_for_mouse == True):
              listener.join()  
  
  #Start new BOT Process
  def startBotProcess():
      
      print(&quot;[SYSTEM] New BOT Process STARTED&quot;)
      
      time_start = time.time()
      
      ps = Process(target=processMain, args=(MOUSE_X,MOUSE_Y))
      ps.start()
      ps.join()
      
      stopBot()
      print(&quot;[SYSTEM] Bot Process CLOSED&quot;)
      
      if (time.time() - time_start &gt; SLEEP):
          print(&quot;[SYSTEM] Restart Timer Created&quot;)
          t = Timer(SLEEP_TIME, botSleep)        
          t.start()
  
  def processMain(x,y):
  
      global MOUSE_X, MOUSE_Y
      MOUSE_X = x
      MOUSE_Y = y
      
      startBot()
  
  def startBot():
  
  print(&quot;[SYSTEM] STARTING BOT - Click Window&quot;)
  
  global running
  global GAME_STATE
  global CUT_SCENE    
  global CHAT_FOUND
  global TRANSITION_TIME
  
  printData()
  
  time.sleep(2)
  running = True
  start_time = time.time()
  
  while (running == True):
      if (GAME_STATE == 0):   #First, lets see if we are in a map
          if (checkMap() == True):
              GAME_STATE = 1
          else:
              print(&quot;GAME_STATE is 0 - Cannot determine if player is on a map&quot;)
              debugger()
  </code></pre>
  <p>Now eventually after ~ 2hours of running the python command prompt will show a fail to allocate memory error
  <a href="https://i.stack.imgur.com/oeRcq.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>The line that its referring too is the cv.matchtemplate.
  Task manager shows the program only using 50mb of memory. Total memory usage on my computer is 51%</p>
  <p>Does anyone know what I can do to resolve this? Task manag</p>
  <p>Updated screenshot with task manager and failed allocation;
  <a href="https://i.stack.imgur.com/V7I1T.png" rel="nofollow noreferrer">enter image description here</a></p>