title: python in VS code : RuntimeError: can't start new thread
body: <p>I'm running into a RuntimeError with my Python 3.7 script in VS code.
  I have different errors if I run it in debug mode (F5) or without debug (ctrl-F5).</p>
  <p>I'm a beginner with Python and VS code. This error is not really meaningful to me.
  When searching for similar question/problem, I've found that it's sometimes linked to trying to launch too many threads. But I don't create thread on purpose, it might be some background stuff though linked to VSCode.
  The code is basicaly a for loop iterating over a dataset and plotting graph, saving it then closing it. So I don't see an obvious memory overusage as I close the plot at the end of each iteration.</p>
  <p>I don't know where to look, or what to monitor to further investigate.
  I'm not posting reproductible code as I believe the error happens on the whole project only, and depends also on the IDE.</p>
  <p>Thanks for your help !</p>
  <p>Here are the error logs with and without debug mode:</p>
  <p>With debug :</p>
  <pre><code>Traceback (most recent call last):
    File &quot;[...]\Python\Python37-32\lib\runpy.py&quot;, line 193, in _run_module_as_main
      &quot;__main__&quot;, mod_spec)
    File &quot;[...]\Python\Python37-32\lib\runpy.py&quot;, line 85, in _run_code
      exec(code, run_globals)
    File &quot;[...]\.vscode\extensions\ms-python.python-2021.11.1422169775\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_trace_dispatch_regular.py&quot;, line 203, in trace_dispatch
      py_db.enable_tracing(thread_trace_func)
    File &quot;[...]\.vscode\extensions\ms-python.python-2021.11.1422169775\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd.py&quot;, line 1066, in enable_tracing
      pydevd_tracing.SetTrace(thread_trace_func)
    File &quot;[...]\.vscode\extensions\ms-python.python-2021.11.1422169775\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_tracing.py&quot;, line 83, in SetTrace
      if set_trace_to_threads(tracing_func, thread_idents=[thread.get_ident()], create_dummy_thread=False) == 0:
    File &quot;[...]\.vscode\extensions\ms-python.python-2021.11.1422169775\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_tracing.py&quot;, line 344, in set_trace_to_threads
      start_new_thread(increase_tracing_count, ())
  RuntimeError: can't start new thread
  </code></pre>
  <p>Without debug :</p>
  <pre><code>Traceback (most recent call last):
    File &quot;[...]main.py&quot;, line 44, in &lt;module&gt;
      visu_rmotor(data, train, debug)
    File &quot;[...]visu_rmotor.py&quot;, line 83, in visu_rmotor
      save_figure('rmotor', train, door)
    File &quot;[...]process_data.py&quot;, line 50, in save_figure
      plt.savefig(save_file_path)
    File &quot;[...]matplotlib\pyplot.py&quot;, line 966, in savefig
      res = fig.savefig(*args, **kwargs)
    File &quot;[...]matplotlib\figure.py&quot;, line 3015, in savefig
      self.canvas.print_figure(fname, **kwargs)
    File &quot;[...]matplotlib\backend_bases.py&quot;, line 2261, in print_figure
      **kwargs)
    File &quot;[...]matplotlib\backend_bases.py&quot;, line 1669, in wrapper
      return func(*args, **kwargs)
    File &quot;[...]matplotlib\backends\backend_agg.py&quot;, line 508, in print_png
      FigureCanvasAgg.draw(self)
    File &quot;[...]matplotlib\backends\backend_agg.py&quot;, line 401, in draw
      self.renderer = self.get_renderer(cleared=True)
    File &quot;[...]matplotlib\backends\backend_agg.py&quot;, line 417, in get_renderer
      self.renderer = RendererAgg(w, h, self.figure.dpi)
    File &quot;[...]matplotlib\backends\backend_agg.py&quot;, line 91, in __init__
      self._renderer = _RendererAgg(int(width), int(height), dpi)
  MemoryError: In RendererAgg: Out of memory
  </code></pre>
  <p>Edit : stripped down my code to this, which will crash at &quot;door=250&quot; and using about 2Gb of RAM</p>
  <pre><code>import matplotlib.pyplot as plt
  
  door=0
  while(1):
      door +=1
      fig, ax = plt.subplots(3, 1, sharex='all', figsize=(11.25, 6), num=10, clear=True)
  
      print(door)
  
      fig.clear() # trying every solution found on stackexchange to unload and free memory !
      fig.clf()
      plt.clf()
      plt.close()                                 
      del fig
  </code></pre>