title: Matplotlib errors result in a memory leak. How can I free up that memory?
body: <p>I am running a django app that includes matplotlib and allows the user to specify the axes of the graph. This can result in <strong>'Overflow Error: Agg complexity exceeded'</strong></p>
  
  <p>When that happens up to 100MB of RAM get tied up. Normally I free that memory up using <code>fig.gcf()</code>, <code>plot.close()</code>, and <code>gc.collect()</code>, but the memory associated with the error does not seem to be associated with the plot object.</p>
  
  <p>Does anyone know how I can release that memory?</p>
  
  <p>Thanks.</p>
  
  <p>Here is some code that gives me the Agg Complexity Error.</p>
  
  <pre><code>import matplotlib
  matplotlib.use('Agg')
  import matplotlib.pyplot as plt
  import numpy as np      
  import gc
  
  a = np.arange(1000000)
  b = np.random.randn(1000000)
  
  fig = plt.figure(num=1, dpi=100, facecolor='w', edgecolor='w')
  fig.set_size_inches(10,7)
  ax = fig.add_subplot(111)
  ax.plot(a, b)
  
  fig.savefig('yourdesktop/random.png')   # code gives me an error here
  
  fig.clf()    # normally I use these lines to release the memory
  plt.close()
  del a, b
  gc.collect()
  </code></pre>