Memory leak; user error or bug?

I'm seeing a nasty memory leak in my strip chart widget using matplotlib
1.0, TkAgg and Mac OS X 10.5

I've posted a minimal version here:
<http://www.astro.washington.edu/users/rowen/python/MinimalStripChartWdg.

It doesn't seem to matter if I use the animation API or not (the example
does not).

Any ideas? If this is a bug I'll report it, but I hope I'm just doing
something wrong.

-- Russell

I don't have a Mac to try this on -- however, I don't see the leak on Fedora 14/Python 2.7/Numpy 1.5.1/Tkinter with both matplotlib 1.0 and matplotlib SVN head. That may narrow it down to something Mac OS X-specific. Sorry that's not totally helpful, but it's a data point.

Mike

···

On 12/01/2010 01:40 PM, Russell E. Owen wrote:

I'm seeing a nasty memory leak in my strip chart widget using matplotlib
1.0, TkAgg and Mac OS X 10.5

I've posted a minimal version here:
<http://www.astro.washington.edu/users/rowen/python/MinimalStripChartWdg.
>

It doesn't seem to matter if I use the animation API or not (the example
does not).

Any ideas? If this is a bug I'll report it, but I hope I'm just doing
something wrong.

2010/12/1 Russell E. Owen <rowen@...2756...>:

I'm seeing a nasty memory leak in my strip chart widget using matplotlib
1.0, TkAgg and Mac OS X 10.5

I've posted a minimal version here:
<http://www.astro.washington.edu/users/rowen/python/MinimalStripChartWdg.
>

It doesn't seem to matter if I use the animation API or not (the example
does not).

Any ideas?

Hmm, you're adding points right? I remember one time I had a similar
timeline updating each second or so, and after 10 hr it took memory of
the order of Gigabytes (iirc) ... and it took seconds to refresh the
plot ... took me some time to figure it out that the plotting was the
culprit ...

hth, fwiw,
Friedrich

I am sort of adding data points; what I'm really doing is appending data to a python list and setting the data in the Line object to that list. The list gets shortened every once in awhile to keep the amount of data from getting too large.

It turns out the leak is in canvas.draw(), which I call whenever I shift the graph in time (by changing the limits on the x axis); if I don't call canvas.draw() the time shift is not displayed. Simply creating a blank Axes and calling canvas.draw() leaks memory -- even without displaying any data or shifting the x axes.

Regards,

-- Russell

···

On Dec 11, 2010, at 1:32 PM, Friedrich Romstedt wrote:

2010/12/1 Russell E. Owen <rowen@...2756...>:

I'm seeing a nasty memory leak in my strip chart widget using matplotlib
1.0, TkAgg and Mac OS X 10.5

I've posted a minimal version here:
<http://www.astro.washington.edu/users/rowen/python/MinimalStripChartWdg.
>

It doesn't seem to matter if I use the animation API or not (the example
does not).

Any ideas?

Hmm, you're adding points right? I remember one time I had a similar
timeline updating each second or so, and after 10 hr it took memory of
the order of Gigabytes (iirc) ... and it took seconds to refresh the
plot ... took me some time to figure it out that the plotting was the
culprit ...

hth, fwiw,
Friedrich

2010/12/12 Russell Owen <rowen@...2756...>:

Simply creating a blank Axes and calling canvas.draw() leaks memory -- even without displaying any data or shifting the x axes.

Okay, as a workaround have you tried ax.clear()?

So the example script provided by you can be boiled down further?

I'm using ax.clear() in a layered approach very extensively and have
never noticed memory leaks with this. I'm having a Layer object which
can be fed by data, and when the data changes, it automatically will
trigger an ax.clear() in the host stack, and then everything is
redrawn. Not the fastest, but fast enough.

Friedrich

I've boiled it down to nearly nothing as a script attached to this bug report:
<https://sourceforge.net/tracker/?func=detail&atid=560720&aid=3124990&group_id=80706>
it simply creates an Axis and then regularly shifts the x axis limits and calls canvas.draw() to display the change. (It also reports memory usage.) You can further simplify the script by commenting out the limes that alter the x axis limits, but then the display does not change (though matplotlib still leaks memory as long as canvas.draw is still called).

I will try ax.clear(). Thanks.

-- Russell

···

On Dec 12, 2010, at 8:08 AM, Friedrich Romstedt wrote:

2010/12/12 Russell Owen <rowen@...2756...>:

Simply creating a blank Axes and calling canvas.draw() leaks memory -- even without displaying any data or shifting the x axes.

Okay, as a workaround have you tried ax.clear()?

So the example script provided by you can be boiled down further?

I'm using ax.clear() in a layered approach very extensively and have
never noticed memory leaks with this. I'm having a Layer object which
can be fed by data, and when the data changes, it automatically will
trigger an ax.clear() in the host stack, and then everything is
redrawn. Not the fastest, but fast enough.

Friedrich

I just tried calling subplot.clear() just before calling canvas.draw(). Interestingly enough it increases the memory leak rate by roughly 50%!

By the way, the minimal script is available here if you want to try it:

<http://www.astro.washington.edu/users/rowen/python/matplotlibMemoryLeak.py>

I would be interested to see your code if it is available. What back end are you using?

Regards,

– Russell

···

On Dec 12, 2010, at 8:08 AM, Friedrich Romstedt wrote:

2010/12/12 Russell Owen <rowen@…2756…>:

Simply creating a blank Axes and calling canvas.draw() leaks memory – even without displaying any data or shifting the x axes.

Okay, as a workaround have you tried ax.clear()?

So the example script provided by you can be boiled down further?

I’m using ax.clear() in a layered approach very extensively and have
never noticed memory leaks with this. I’m having a Layer object which
can be fed by data, and when the data changes, it automatically will
trigger an ax.clear() in the host stack, and then everything is
redrawn. Not the fastest, but fast enough.

Friedrich