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@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to