How can I release memory after creating matplotlib figures

PythonMemory LeaksNumpyMatplotlib

Python Problem Overview


I have several matlpotlib functions rolled into some django-celery tasks.

Every time the tasks are called more RAM is dedicated to python. Before too long, python is taking up all of the RAM.

QUESTION: How can I release this memory?

UPDATE 2 - A Second Solution:

I asked a similar question specifically about the memory locked up when matplotlib errors, but I got a good answer to this question .clf(), .close(), and gc.collect() aren't needed if you use multiprocess to run the plotting function in a separate process whose memory will automatically be freed once the process ends.

https://stackoverflow.com/questions/7125710/matplotlib-errors-result-in-a-memory-leak-how-can-i-free-up-that-memory

UPDATE - The Solution:

These stackoverflow posts suggested that I can release the memory used by matplotlib objects with the following commands:

.clf(): https://stackoverflow.com/questions/2364945/matplotlib-runs-out-of-memory-when-plotting-in-a-loop-edited

.close(): https://stackoverflow.com/questions/3623600/python-matplotlib-memory-not-being-released-when-specifying-figure-size

import gc
gc.collect()

Here is the example I used to test the solution:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from pylab import import figure, savefig
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.clf()
plt.close()
del a, b
gc.collect()


Python Solutions


Solution 1 - Python

Did you try to run you task function several times (in a for) to be sure that not your function is leaking no matter of celery? Make sure that django.settings.DEBUG is set False( The connection object holds all queries in memmory when DEBUG=True).

Solution 2 - Python

import matplotlib.pyplot as plt
from datetime import datetime
import gc

class MyClass:
    def plotmanytimesandsave(self):
        plt.plot([1, 2, 3])
        ro2 = datetime.now()
        f =ro2.second
        name =str(f)+".jpg"
        plt.savefig(name)
        plt.draw()
        plt.clf()
        plt.close("all")


for y in range(1, 10):
    k = MyClass()
    k.plotmanytimesandsave()
    del k
    k = "now our class object is a string"
    print(k)
    del k
    gc.collect

with this program you will save directly as many times you want without the plt.show() command. And the memory consumption will be low.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsequoiaView Question on Stackoverflow
Solution 1 - PythonNicolae DascaluView Answer on Stackoverflow
Solution 2 - PythonMarios StaboulasView Answer on Stackoverflow