Matplotlib errors result in a memory leak. How can I free this memory?

I am running a django application that includes matplotlib and allows the user to indicate the axis of the graph. This can lead to "Overflow Error: Agg Complexity Exceeded"

When this happens, up to 100 MB of RAM are tied. I usually free this memory with fig.gcf() , plot.close() and gc.collect() , but the memory associated with this error does not seem to be related to the plot object.

Does anyone know how I can free this memory?

Thank.

Here is the code that gives me an Agg complexity error.

 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() 
+9
python matplotlib memory-leaks
Aug 19 '11 at 18:20
source share
2 answers

I assume that you can run the code that you published at least once. The problem only appears after running the published code multiple times. Right?

If so, the following resolves the problem without identifying the source of the problem. This may be bad, but it works as a last resort: just use multiprocessing to run memory-intensive code in a separate process. You do not need to worry about fig.clf() or plt.close() or del a,b or gc.collect() . All memory is freed upon completion of the process.

 import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import numpy as np import multiprocessing as mp def worker(): N=1000000 a = np.arange(N) b = np.random.randn(N) 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('/tmp/random.png') # code gives me an error here if __name__=='__main__': proc=mp.Process(target=worker) proc.daemon=True proc.start() proc.join() 

You also don't need proc.join() . join blocks the main process until worker completes. If you omit join , then the main process will simply continue with the worker process running in the background.

+9
Aug 19 '11 at 18:33
source share

I find here http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg11809.html , it gives an interesting answer that may help

try replacing:

 import matplotlib.pyplot as plt fig = plt.figure() 

from

 from matplotlib import figure fig = figure.Figure() 
+11
Sep 06 '12 at 12:20
source share



All Articles