Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I release memory after creating matplotlib figures

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.

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(): Matplotlib runs out of memory when plotting in a loop

.close(): 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() 
like image 769
sequoia Avatar asked Aug 18 '11 01:08

sequoia


People also ask

How do I save a matplotlib figure without white space?

Hide the Whitespaces and Borders in Matplotlib Figure To get rid of whitespace around the border, we can set bbox_inches='tight' in the savefig() method. Similarly, to remove the white border around the image while we set pad_inches = 0 in the savefig() method.

How do I save matplotlib figures with high resolution?

To save the file in pdf format, use savefig() method where the image name is myImagePDF. pdf, format="pdf". We can set the dpi value to get a high-quality image. Using the saving() method, we can save the image with format=”png” and dpi=1200.


2 Answers

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).

like image 80
Nicolae Dascalu Avatar answered Sep 19 '22 14:09

Nicolae Dascalu


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.

like image 36
Marios Staboulas Avatar answered Sep 20 '22 14:09

Marios Staboulas