-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Memory leak with log scale in pcolorfast, pcolormesh, imshow ... #15474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It’s possible a transform isn’t being cleaned up. How bad is the leak? |
Using Our application, with less frequent updates, takes a few minutes to claim all the memory. |
Agreed, this leaks on my machine using master. |
Memory leak is real, is there any resolution for this issue or any temporary solution to bypass before new release? ...\site-packages\matplotlib\image.py:431: size=141 MiB, count=79, average=1833 KiB
...\site-packages\matplotlib\image.py:466: size=141 MiB, count=52, average=2785 KiB |
I guess a work around is to animate by saving pngs and then calling ffmpegs or other writers. |
We talked about this on the call and we suspect that the memory leak is in the c++ based resampling code for |
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 diff --git i/src/_image_wrapper.cpp w/src/_image_wrapper.cpp
index de65e90c8..cb9a91ce0 100644
--- i/src/_image_wrapper.cpp
+++ w/src/_image_wrapper.cpp
@@ -146,9 +146,13 @@ image_resample(PyObject *self, PyObject* args, PyObject *kwargs)
goto error;
}
- output_array = (PyArrayObject *)PyArray_FromAny(
- py_output_array, NULL, 2, 3, NPY_ARRAY_C_CONTIGUOUS, NULL);
- if (output_array == NULL) {
+ if (!PyArray_Check(py_output_array)) {
+ PyErr_SetString(PyExc_ValueError, "output array must be a numpy array");
+ goto error;
+ }
+ output_array = (PyArrayObject *)py_output_array;
+ if (!PyArray_IS_C_CONTIGUOUS(output_array)) {
+ PyErr_SetString(PyExc_ValueError, "output array must be a C-contiguous");
goto error;
}
@@ -330,11 +334,10 @@ image_resample(PyObject *self, PyObject* args, PyObject *kwargs)
Py_DECREF(input_array);
Py_XDECREF(transform_mesh_array);
- return (PyObject *)output_array;
+ Py_RETURN_NONE;
error:
Py_XDECREF(input_array);
- Py_XDECREF(output_array);
Py_XDECREF(transform_mesh_array);
return NULL;
} (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. |
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. |
Thanks for reporting this @ibancg ! |
Great, thanks for the fix!. |
matplotlib seems to leak memory when I switch to log scale on the Y axis, for the functions
pcolorfast
,pcolormesh
,imshow
andspecgram
Please consider the following spectrogram animation (sorry for the extra dependencies to
scipy
)Everything stays stable if I stick to linear scale on the Y axis, commenting out the line
I can reproduce it with matplotlib
3.1.1
(viapip
) and previous versions (tested with3.0.2
from system-wide Debian testing package). Also I am usingQt5Agg
backend andpython 3.7.5rc1
The text was updated successfully, but these errors were encountered: