Memory Leak with Tkinter Canvas (Python 2.5 Win32)

Discussion in 'Python' started by frikk, Aug 2, 2007.

  1. frikk

    frikk Guest

    Hey everyone. I have been working with python for a couple years now,
    but just recently built my first program with a GUI. I decided to
    start with Tkinter since it is included with the base package,
    although wxWindows will likely be my next choice. Tkinter seems to be
    pretty slow for my needs.

    Anyway - I am building a genetic algorithm simulator. I have a grid
    where an Ant moves around. It is infeasible for me to update the grid
    every simulation step - so I just do it at the end. But what I've
    realized is that my program performs worse and worse when I update the
    grid. Turns out there is a memory leak somewhere and I don't think it
    is in my code. The memory leak occurs only when I write (via
    create_rectangle) to the canvas widget. I wrote the following small
    script to demonstrate this problem (see below). Every time the button
    is pressed, _1040KB_ is added to the RAM of wpython.exe. This adds up
    FAST. I have not verified this on my OS X box.

    As you can see- I am doing nothing other than drawing a lot of
    rectangles on the canvas. I have two questions.

    1. Is this a bug in my usage of Tkinter? Am I somehow leaving
    objects laying around that aren't being deleted? Is create_rectangle
    not the appropriate function to use?)
    2. Is there a better, quicker way to update a "grid"-like object?

    Thanks,
    Blaine

    Current System:
    Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    (Intel)] on win32
    Windows XP SP2 - all recent patches and updates

    Script:

    from Tkinter import *

    canv = None
    HEIGHT=600
    WIDTH=600
    def clear_grid():
    for i in range(0,HEIGHT/10):
    for j in range(0, HEIGHT/10):
    canv.create_rectangle(i*10,j*10, \
    i*10+10, j*10+10, \
    fill = "white")

    def draw_window(master):
    global canv
    frame = Frame(master)

    btn_grid = Button(frame, text="draw grid", command=clear_grid)
    btn_grid.pack(side=TOP)

    canv = Canvas(frame, height=HEIGHT, width=WIDTH, bg='white')
    canv.pack()
    frame.pack()

    root = Tk()
    draw_window(root)
    mainloop()
     
    frikk, Aug 2, 2007
    #1
    1. Advertising

  2. frikk wrote:
    > [...]
    > As you can see- I am doing nothing other than drawing a lot of
    > rectangles on the canvas.


    You aren't drawing, but **creating** rectangle objects, as
    a meth. name suggests. You find more info at tkinter.effbot.org.

    > [...]
    >
    > def clear_grid():


    canv.delete(ALL)

    > for i in range(0,HEIGHT/10):
    > for j in range(0, HEIGHT/10):
    > canv.create_rectangle(i*10,j*10, \
    > i*10+10, j*10+10, \
    > fill = "white")


    w.
     
    Wojciech =?iso-8859-2?Q?Mu=B3a?=, Aug 2, 2007
    #2
    1. Advertising

  3. "frikk" <fr...l.com> wrote:

    > 1. ....... Am I somehow leaving
    > objects laying around that aren't being deleted? Is create_rectangle
    > not the appropriate function to use?)


    Try calling the canvas's delete method with the old rectangle before
    making a new one.

    - Hendrik
     
    Hendrik van Rooyen, Aug 3, 2007
    #3
  4. frikk

    frikk Guest

    On Aug 3, 2:26 am, "Hendrik van Rooyen" <> wrote:
    > "frikk" <fr...l.com> wrote:
    > > 1. ....... Am I somehow leaving
    > > objects laying around that aren't being deleted? Is create_rectangle
    > > not the appropriate function to use?)

    >
    > Try calling the canvas's delete method with the old rectangle before
    > making a new one.
    >
    > - Hendrik


    Hey guys-

    Both of your suggestions were perfect - I was just unaware I was
    actually creating new *object* instead of drawing on the canvas. noob
    mistake. Thanks again!

    -Blaine
     
    frikk, Aug 3, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ton K.
    Replies:
    0
    Views:
    366
    Ton K.
    Jul 25, 2003
  2. Elbert Lev

    Memory leak in Tkinter?????

    Elbert Lev, Jul 23, 2004, in forum: Python
    Replies:
    2
    Views:
    334
    Elbert Lev
    Jul 25, 2004
  3. Askari
    Replies:
    2
    Views:
    717
    Askari
    Aug 30, 2004
  4. PhilC
    Replies:
    2
    Views:
    896
    PhilC
    Oct 25, 2004
  5. Replies:
    10
    Views:
    273
    Rick Johnson
    Mar 15, 2013
Loading...

Share This Page