You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Memory leak with log scale in pcolorfast, pcolormesh, imshow ... #15474
matplotlib seems to leak memory when I switch to log scale on the Y axis, for the functions pcolorfast, pcolormesh, imshow and specgram
Please consider the following spectrogram animation (sorry for the extra dependencies to scipy)
fromscipy.signal.spectralimportspectrogramfromscipy.signal.waveformsimportchirpimportmatplotlib.animationasanimationimportmatplotlib.pyplotaspltimportnumpyasnpfs=8000# sampling ratet=np.arange(0, 5.0, 1/fs) # 5 seconds of timefig, axes=plt.subplots(nrows=1)
defsignal(t): # generates a "chirp" signalreturnchirp(t=t, f0=100.0, f1=3000.0, t1=10.0)
defspect(x): # computes spectrogramfreq, time, Sxx=spectrogram(x, fs=fs, nfft=1024, noverlap=512, nperseg=1024)
Z=10*np.log10(Sxx)
returnfreq, time, Zy=signal(t)
freq, time, Z=spect(y)
im=axes.pcolorfast(time, freq, Z)
axes.set_xlabel('t [s]')
axes.set_ylabel('f [Hz]')
axes.set_yscale('log') # <-- exposes the leakaxes.set_ylim(0, fs/2)
defanimate(i):
y=signal(t+i/30.0) # sliding temporal windowfreq, time, Z=spect(y)
im.set_data(Z)
return [ im ]
ani=animation.FuncAnimation(fig, animate, np.arange(1, 200),
interval=0, blit=True)
plt.show()
Everything stays stable if I stick to linear scale on the Y axis, commenting out the line
axes.set_yscale('log')
I can reproduce it with matplotlib 3.1.1 (via pip) and previous versions (tested with 3.0.2 from system-wide Debian testing package). Also I am using Qt5Agg backend and python 3.7.5rc1
Using psutil I see that the heap usage in this particular example stays stable at 145 MB with linear scale, but it eats up 2 GB in less than 20 seconds for the log scale.
Our application, with less frequent updates, takes a few minutes to claim all the memory.
It was right to look at the C++ code, but it wasn't output array that leaking. I included that change as part of general cleanup, but the real leak was the input mesh.
Activity
jklymak commentedon Oct 22, 2019
It’s possible a transform isn’t being cleaned up. How bad is the leak?
ibancg commentedon Oct 22, 2019
Using
psutilI see that the heap usage in this particular example stays stable at 145 MB with linear scale, but it eats up 2 GB in less than 20 seconds for the log scale.Our application, with less frequent updates, takes a few minutes to claim all the memory.
jklymak commentedon Oct 22, 2019
Agreed, this leaks on my machine using master.
espehannila commentedon Mar 10, 2020
Memory leak is real, is there any resolution for this issue or any temporary solution to bypass before new release?
jklymak commentedon Mar 10, 2020
I guess a work around is to animate by saving pngs and then calling ffmpegs or other writers.
tacaswell commentedon May 18, 2020
We talked about this on the call and we suspect that the memory leak is in the c++ based resampling code for
AxesImage.anntzer commentedon May 18, 2020
Upon inspection it looks like the issue may be(?) in _image_wrapper.cpp::image_resample, specifically at
output_array = (PyArrayObject *)PyArray_FromAny( py_output_array, NULL, 2, 3, NPY_ARRAY_C_CONTIGUOUS, NULL); if (output_array == NULL) {where the problem is that we already allocated an array in python code and allocate another PyObject here(?); however the obvious
(just getting the array from outside and making sure it has the right size and not messing with its refcounts at all) does not seem to fix the issue.
Fix leak of input mesh during image resampling.
QuLogic commentedon May 20, 2020
It was right to look at the C++ code, but it wasn't output array that leaking. I included that change as part of general cleanup, but the real leak was the input mesh.
Fix leak of input mesh during image resampling.
tacaswell commentedon May 21, 2020
Thanks for reporting this @ibancg !
ibancg commentedon May 21, 2020
Great, thanks for the fix!.