I use matplotlib to generate many graphs of numerical simulation results. Graphs are used as frames in the video, and so I generate many of them by repeatedly calling a function similar to this:
from pylab import * def plot_density(filename,i,t,psi_Na): figure(figsize=(8,6)) imshow(abs(psi_Na)**2,origin = 'lower') savefig(filename + '_%04d.png'%i) clf()
The problem is that the memory usage for the python process increases by a couple megabytes each time this function is called. For example, if I call it with this loop:
if __name__ == "__main__": x = linspace(-6e-6,6e-6,128,endpoint=False) y = linspace(-6e-6,6e-6,128,endpoint=False) X,Y = meshgrid(x,y) k = 1000000 omega = 200 times = linspace(0,100e-3,100,endpoint=False) for i,t in enumerate(times): psi_Na = sin(k*X-omega*t) plot_density('wavefunction',i,t,psi_Na) print i
then plunger usage grows with time to 600 MB. If, however, I comment on the line figure(figsize=(8,6)) in the definition of the function, then the use of the plunger remains stable at 52 MB. (8,6) is the default shape size, and therefore the same image is created in both cases. I would like to make graphs of different sizes from my numerical data without being exhausted. How can I get python to free this memory?
I tried gc.collect() every cycle to force garbage collection, and I tried f = gcf() to get the current digit and then del f to delete it, but to no avail.
I am running CPython 2.6.5 on 64 bit Ubuntu 10.04.
python memory-management matplotlib
Chris Billington Sep 02 2018-10-09T00: 00Z
source share