Memory leak in Tkinter?????

Discussion in 'Python' started by Elbert Lev, Jul 23, 2004.

  1. Elbert Lev

    Elbert Lev Guest

    #When I'm running this script on my windows NT4.0 box,
    #every time dialog box is reopened there is memory growth 384K.

    #Bellow is the text I sent to Stephen Ferg (author of easygui)

    # I have tested the pure Tkinter,
    # by modifiing on of the examples in the distribution.
    # This little guy also exibits the same behaviour.
    # Namely: every time the window is closed and reoppend,
    # there is memory leak of several hundreds 384K
    # (good number??? 256 + 128??? or it's windows?)
    # del root does not help at all.
    # Anyway, I don't think that this leak is in easygui.
    # The leak (if any is in TKinter (or it's windows implementation).
    # I'm not runnin linux in my shop, so I will ask my friends to tes it on linux,
    # will keep you posted.

    from Tkinter import *
    import string

    # This program shows how to use a simple type-in box

    class App(Frame):
    def __init__(self, master=None):
    Frame.__init__(self, master)
    self.pack()

    self.entrythingy = Entry()
    self.entrythingy.pack()

    # and here we get a callback when the user hits return. we could
    # make the key that triggers the callback anything we wanted to.
    # other typical options might be <Key-Tab> or <Key> (for anything)
    self.entrythingy.bind('<Key-Return>', self.print_contents)

    def print_contents(self, event):
    print "hi. contents of entry is now ---->", self.entrythingy.get()

    while 1:
    root = App()
    root.master.title("Foo")
    root.mainloop()
    del root

    #I found this memory leak while working with easygui.
    #Initially I thought, that the leak is caused by the way easygui opens
    #and closes the window/application.
    #But plain Tkinter also has leaks. What is wrong?
    #A friend of mine ran this script on his MAC and here is his reply:

    ##Yep, I'm seeing a .32 MB leak whenever I close the window and it
    ##reopens. Also seeing a high number of page faults for that process
    ##that increase when it is closed, so I think the memory for the window
    ##is not being deallocated, then Python hits an exception after it page
    ##faults trying to access it, and it then spawns a new one.
     
    Elbert Lev, Jul 23, 2004
    #1
    1. Advertising

  2. Elbert Lev

    klappnase Guest

    (Elbert Lev) wrote in message news:<>...
    > #When I'm running this script on my windows NT4.0 box,
    > #every time dialog box is reopened there is memory growth 384K.
    >
    > #Bellow is the text I sent to Stephen Ferg (author of easygui)
    >
    > # I have tested the pure Tkinter,
    > # by modifiing on of the examples in the distribution.
    > # This little guy also exibits the same behaviour.
    > # Namely: every time the window is closed and reoppend,
    > # there is memory leak of several hundreds 384K
    > # (good number??? 256 + 128??? or it's windows?)
    > # del root does not help at all.
    > # Anyway, I don't think that this leak is in easygui.
    > # The leak (if any is in TKinter (or it's windows implementation).
    > # I'm not runnin linux in my shop, so I will ask my friends to tes it on linux,
    > # will keep you posted.
    >


    I don't see a memory leak here on my linux box; there's a noticeable
    increase of memory use if I close/reopen the window a few times within
    a couple of seconds, but after about 5 seconds it disappears, so it
    looks like python's garbage collection works properly (are you aware
    that calling "del" does not free the memory immediately but just
    remove the reference to the deleted object so it can be garbage
    collected later on).

    > from Tkinter import *
    > import string
    >
    > # This program shows how to use a simple type-in box


    I'm not sure what you want to do with this program, looks rather like
    a useless type-in box to me.

    >
    > class App(Frame):
    > def __init__(self, master=None):
    > Frame.__init__(self, master)
    > self.pack()
    >
    > self.entrythingy = Entry()
    > self.entrythingy.pack()
    >
    > # and here we get a callback when the user hits return. we could
    > # make the key that triggers the callback anything we wanted to.
    > # other typical options might be <Key-Tab> or <Key> (for anything)
    > self.entrythingy.bind('<Key-Return>', self.print_contents)
    >
    > def print_contents(self, event):
    > print "hi. contents of entry is now ---->", self.entrythingy.get()
    >
    > while 1:
    > root = App()
    > root.master.title("Foo")
    > root.mainloop()
    > del root
    >


    Why do you use the "while 1" condition to create your "dialog box"?
    Notice that you create and endless loop without any "break" condition,
    there's no way to stop the program from the gui itself which is
    definitely no good idea.
    Besides this you should be very careful using "while 1" statements in
    any case.
    For example try this:

    >>> def test():

    .... l = []
    .... while 1:
    .... l.append(1)
    ....
    >>> test()


    The result is of course not a python bug but a programmer's mistake.

    > #I found this memory leak while working with easygui.
    > #Initially I thought, that the leak is caused by the way easygui opens
    > #and closes the window/application.
    > #But plain Tkinter also has leaks. What is wrong?


    I don't have windows or mac here, but on my linux box it looks like
    the only thing that's wrong is the code.

    > #A friend of mine ran this script on his MAC and here is his reply:
    >
    > ##Yep, I'm seeing a .32 MB leak whenever I close the window and it
    > ##reopens. Also seeing a high number of page faults for that process
    > ##that increase when it is closed, so I think the memory for the window
    > ##is not being deallocated, then Python hits an exception after it page
    > ##faults trying to access it, and it then spawns a new one.


    Michael
     
    klappnase, Jul 25, 2004
    #2
    1. Advertising

  3. Elbert Lev

    Elbert Lev Guest

    (klappnase) wrote in message news:<>...
    > (Elbert Lev) wrote in message news:<>...


    > I don't see a memory leak here on my linux box; there's a noticeable
    > increase of memory use if I close/reopen the window a few times within
    > a couple of seconds, but after about 5 seconds it disappears, so it
    > looks like python's garbage collection works properly (are you aware
    > that calling "del" does not free the memory immediately but just
    > remove the reference to the deleted object so it can be garbage
    > collected later on).


    I don't think this is a correct statement. Python uses refference
    counting. So, as soon as rc goes to 0, object is deleted. This can be
    done several ways:

    a = list(1,2,3)
    del a # a is deleted

    or

    a = list(1,2,3)
    a = 0 # list content is deleted and new object is created

    See Language Reference 3.1 for more details.

    Python isn't Java and DOES NOT HAVE lazy garbage collection (but you
    can call it manualy. In this case ciclic references are counted and
    resolved). I checked them and the number of items in gc.garbage was 0

    > Why do you use the "while 1" condition to create your "dialog box"?
    > Notice that you create and endless loop without any "break" condition,
    > there's no way to stop the program from the gui itself which is
    > definitely no good idea.
    > Besides this you should be very careful using "while 1" statements in
    > any case.

    ..................
    > I don't have windows or mac here, but on my linux box it looks like
    > the only thing that's wrong is the code.


    Sure while 1: is a test case, sure the program shows a simple dialog
    box, but you missed the point - this is a test case to check if there
    is a leak or not.

    I repeated the test in windows and I do not see "delayd release".
     
    Elbert Lev, Jul 25, 2004
    #3
    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. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    707
    Eric Sosman
    Mar 22, 2005
  3. frikk
    Replies:
    3
    Views:
    323
    frikk
    Aug 3, 2007
  4. cham
    Replies:
    5
    Views:
    772
  5. André
    Replies:
    2
    Views:
    634
    André
    Dec 31, 2008
Loading...

Share This Page