title: Python - Memory leak when checking the size of a multiprocessing SyncManager Queue in a transient thread
body: <p>In a larger python program I noticed a memory leak relating to threading and checking the size of a <a href="https://docs.python.org/2/library/multiprocessing.html#multiprocessing.managers.SyncManager" rel="nofollow">multiprocessing SyncManager</a> Queue (perhaps other operations too?).</p>
  
  <p>In the program a timed thread acts like a monitor and reports the size of the Queue (which we can then decide if additional workers need to be started etc...).</p>
  
  <p>I have boiled down the code to the small script below that demonstrates the issue (with both python2 and python3).  From what I can tell, it seems that when the .qsize() call is made it must allocate some memory which then doesn't get cleaned up when that thread exits?</p>
  
  <p>Is there an obvious flaw in my code or is this simply a case of "don't do this in a thread"?</p>
  
  <p>I did notice the <a href="https://docs.python.org/2/library/multiprocessing.html#programming-guidelines" rel="nofollow">Thread safety of proxies -- to protect with a lock.</a> However I don't feel like this is really a case of that.</p>
  
  <p>Here is the example program that exhibits the behavior.</p>
  
  <pre><code>#!/usr/bin/env python
  import time
  import threading
  import resource
  from multiprocessing.managers import SyncManager
  
  
  def threadCheckSize():
      # apparently calling this in a thread that will soon be going away causes memory leaks?
      worker_queue.qsize()
  
  
  manager = SyncManager()
  manager.start()
  
  worker_queue = manager.Queue()
  
  print('Starting test!')
  
  last_time = time.time() - 20
  while True:
      if time.time() &gt; last_time + 10:
          print('Memory usage: %s (kb)' % resource.getrusage(resource.RUSAGE_SELF).ru_maxrss)
          last_time = time.time()
  
      my_timer = threading.Thread(target=threadCheckSize)
      my_timer.daemon = True
      my_timer.start()
      my_timer.join()
  </code></pre>