memory leak in canvas.draw() in TkAgg

I already posted results using the svn trunk plus the svn trunk with
your patch (as well as the 1.0.0 release). I updated them today with a
few more options (such as your patch without setting the x limit -- it
didn't make any difference) and letting them run longer. Here they are.
All are from running
<http://www.astro.washington.edu/users/rowen/python/matplotlibMemoryLeak.

with various lines commented out (listed before the output). All of

these are on Mac OS X using python.org Python 2.6.6 and TkAgg with
Tcl/Tk 8.4.19.

I have confirmed the leak on linux using matplotlib 1.0.0 (indeed it is
even worse on linux than on Mac OS X -- closer to 28kb/sec than 20) but
have not tried building svn versions on linux.

-- Russell

matplotlib 1.0.0

        self.subplot.set_xlim(tMin, tMax)
# self.subplot.clear()
        self.canvas.draw()
rowen:Python bugs rowen$ python matplotlibMemoryLeak.py
time rss memory mem/sec
(sec) (kb) (kb/sec)
   0.0 27676 nan
   5.0 28980 nan
  10.1 29084 20.6
  15.1 29172 19.0
  20.1 29268 19.1
  25.2 29368 19.3
  30.2 29464 19.2
  35.2 29564 19.4
  40.2 29660 19.3
  45.2 29764 19.5
...
1521.6 58248 19.3
1526.6 58348 19.3

        self.subplot.set_xlim(tMin, tMax)
        self.subplot.clear()
        self.canvas.draw()
rowen:Python bugs rowen$ python matplotlibMemoryLeak.py
time rss memory mem/sec
(sec) (kb) (kb/sec)
   0.0 27696 nan
   5.1 29012 nan
  10.2 29152 27.5
  15.2 29292 27.7
  20.2 29436 28.0
  25.3 29580 28.1
  30.4 29728 28.3
  35.4 29876 28.5
  40.5 30024 28.6
  45.5 30160 28.4
  50.6 30308 28.5

matplotlib svn trunk rev8836

        self.subplot.set_xlim(tMin, tMax)
# self.subplot.clear()
        self.canvas.draw()
rowen:Python bugs rowen$ python matplotlibMemoryLeak.py
time rss memory mem/sec
(sec) (kb) (kb/sec)
   0.0 27664 nan
   5.0 28864 nan
  10.0 28880 3.2
  15.1 28884 2.0
  20.1 28896 2.1
  25.1 28908 2.2
  30.1 28916 2.1
  35.1 28928 2.1
  40.2 28940 2.2
  45.2 28948 2.1

        self.subplot.set_xlim(tMin, tMax)
        self.subplot.clear()
        self.canvas.draw()
time rss memory mem/sec
(sec) (kb) (kb/sec)
   0.0 27684 nan
   5.1 28856 nan
  10.1 28856 0.0
  15.1 28856 0.0
  20.1 28864 0.5
  25.2 28872 0.8
  30.2 28880 1.0
  35.2 28884 0.9
  40.2 28892 1.0
  45.2 28900 1.1
  50.2 28920 1.4
  55.3 28924 1.4
  60.3 28932 1.4
  65.3 28940 1.4

After applying Michael Droettboom's patch to svn trunk rev8836:
--- lib/matplotlib/text.py (revision 8819)
+++ lib/matplotlib/text.py (working copy)
@@ -143,6 +143,9 @@
    Handle storing and drawing of text in window or data coordinates.
    """
    zorder = 3

···

+
+ cached = maxdict(50)
+
    def __str__(self):
        return "Text(%g,%g,%s)"%(self._y,self._y,repr(self._text))

@@ -168,7 +171,6 @@
        """

        Artist.__init__(self)
- self.cached = maxdict(5)
        self._x, self._y = x, y

        if color is None: color = rcParams['text.color']

        # the preferred code
        self.subplot.set_xlim(tMin, tMax)
# self.subplot.clear()
        self.canvas.draw()
rowen:Python bugs rowen$ python matplotlibMemoryLeak.py
time rss memory mem/sec
(sec) (kb) (kb/sec)
   0.0 27656 nan
   5.0 28852 nan
  10.1 28864 2.4
  15.1 28876 2.4
  20.1 28888 2.4
  25.1 28908 2.8
  30.1 28916 2.6
  35.1 28924 2.4
  40.2 28936 2.4
...
803.1 30296 1.8
808.1 30304 1.8
...
2625.1 33628 1.8
2630.1 33640 1.8
...
4186.1 36476 1.8
4191.2 36488 1.8
...
6228.9 40228 1.8
6233.9 40236 1.8

        self.subplot.set_xlim(tMin, tMax)
        self.subplot.clear()
        self.canvas.draw()
rowen:Python bugs rowen$ python matplotlibMemoryLeak.py
time rss memory mem/sec
(sec) (kb) (kb/sec)
   0.0 27652 nan
   5.0 28836 nan
  10.0 28852 3.2
  15.1 28852 1.6
  20.1 28872 2.4
  25.1 28880 2.2
  30.1 28900 2.6
  35.1 28904 2.3
  40.2 28912 2.2
  45.2 28920 2.1
  50.3 28928 2.0

# self.subplot.set_xlim(tMin, tMax)
# self.subplot.clear()
        self.canvas.draw()
rowen:Python bugs rowen$ python matplotlibMemoryLeak.py
time rss memory mem/sec
(sec) (kb) (kb/sec)
   0.0 27636 nan
   5.0 28832 nan
  10.0 28840 1.6
  15.1 28840 0.8
  20.1 28852 1.3
  25.1 28860 1.4
  30.1 28868 1.4
  35.1 28876 1.5
  40.1 28888 1.6
  45.1 28908 1.9
...
447.0 29620 1.8
452.1 29628 1.8
...
1180.3 30940 1.8
1185.3 30948 1.8

In article <4D07A8A0.5000601@...31...>,
Michael Droettboom <mdroe@...31...>
wrote:

If you're able to try this with the 1.0.x SVN branch or the SVN trunk
(plus my text patch) let me know. I am not able to reproduce any sort
of memory leak with these newer versions, but I am able to with 1.0.0 as
released (with or without my text patch). This is with or without the x
axis limits updating you suggested. I believe there have been other
memory leak fixes since the 1.0.0 release.

Cheers,
Mike

On 12/13/2010 08:35 PM, Russell E. Owen wrote:
> I also wanted to say:
> - Thank you for the patch. I appreciate the chance to try a fix.
> - I doubt the issue is related to text because my scripts shows a memory
> leak even if the displayed text is never updated (comment out the line
> that modifies the x axis limits to see what I mean; though I confess I
> did not try that with the trunk matplotlib, only with 1.0.0).
>
> -- Russell
>
> In article<4D028BC7.1000304@...31...>,
> Michael Droettboom<mdroe@...31...>
> wrote:
>
>
>> I think I'm on to something -- it seems that text layout information has
>> a cyclical reference that prevents the Text object from being freed.
>>
>> Can you apply the attached patch and let me know if it solves your issue?
>>
> ...
>
>
> ----------------------------------------------------------------------------
> --
> Lotusphere 2011
> Register now for Lotusphere 2011 and learn how
> to connect the dots, take your collaborative environment
> to the next level, and enter the era of Social Business.
> http://p.sf.net/sfu/lotusphere-d2d
> _______________________________________________
> Matplotlib-devel mailing list
> Matplotlib-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
>

I