[Tkinter-discuss] Memory leak with tkinter canvas itemconfigure

Michael O'Donnell michael.odonnell at uam.es
Fri Nov 12 04:40:23 CET 2010


Hi John,

  I confirm your memory leakage on the code you sent,
on a Windows 7 machine running python 2.7 / Tk 8.5

And when one comments out the c.itemconfigure line, memory usage is
constant, so the problem is there.

And looking in Tkinter.py, the itemconfigure method on a Canvas object
calls _configure on the base class, but the problem is not on the Tkinter
side, it is in Tk.

Mick

On Thu, Nov 11, 2010 at 11:32 PM, John McMonagle <jmcmonagle at velseis.com> wrote:
> Hi,
>
> I have an application that configures thousands of canvas rectangle
> items per second and I have noticed that the memory usage leaks badly.
>
> The code below illustrates the problem:
>
> #---------------------------------------------------------------
> import Tkinter as tk
> import random
>
> r = tk.Tk()
>
> c = tk.Canvas(r)
> c.pack(fill=tk.BOTH, expand=tk.YES)
>
> # draw 400 rectangles
> objs = []
> for i in range(20):
>    for j in range(20):
>        obj = c.create_rectangle(i*10, j*10, i*10+10, j*10+10,
> fill='#888888', outline='#888888')
>        objs.append(obj)
>
> def run():
>    # loop indefinitely and randomly change the canvas item colours
>    while True:
>        obj = random.choice(objs)
>        red = random.randint(0,255)
>        green = random.randint(0,255)
>        blue = random.randint(0,255)
>        colour = '#%02x%02x%02x' %(red, green, blue)
>        c.itemconfigure(obj, fill=colour, outline=colour)
>        r.update()
>
> tk.Button(r, text='Run', command=run).pack()
> tk.Button(r, text='Close', command=r.destroy).pack()
>
> r.mainloop()
> #----------------------------------------------------------------------
>
> So as you can see, each iteration of the while loop just does an
> itemconfigure on a random item on the canvas and then an update to
> process the redraw.
>
> I've tried periodically deleting the canvas items and redrawing them and
> even periodically destroying the Canvas object itself and restarting
> from scratch.  Alas, memory is never released.  So, I come to the
> conclusion that the memory leak must be in the Tcl/Tk interpreter.
>
> Is there any way of forcing the Tcl/Tk interpreter to release memory ?
>
> Can anyone help shed some light on this mystery ?
>
> Regards,
>
> John McMonagle
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>


More information about the Tkinter-discuss mailing list