Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

memory leaks in Tkinter

379 views
Skip to first unread message

Maric MICHAUD

unread,
Nov 26, 2002, 9:21:11 AM11/26/02
to
I noticied some memory leak in text widget of Tkinter :

Python 2.1.1 (#1, Aug 30 2001, 17:36:05)
[GCC 2.96 20000731 (Mandrake Linux 8.1 2.96-0.61mdk)] on linux-i386
Type "copyright", "credits" or "license" for more information.
>>> s=open("a big file").read()
>>> import Tkinter
>>> t=Tkinter.Tk()
>>> x=Tkinter.Text(t)
>>> x.pack()
>>> x.insert(end, s) # growing up memory to a little
more than size of s
>>> x.delete(1.0, end) # does not free memory
>>> x.insert(end, s) # memory goes up again
>>> x.delete(1.0, end) # memory freed by only one s
>>>

Is this a bug ? A problem with the version 1.152 of Tkinter ?

Jeff Epler

unread,
Nov 26, 2002, 1:04:01 PM11/26/02
to
I think the term "memory leak" is usually used when something can make
the process use an unbounded amount of memory over multiple iterations.
Here, the amount of memory consumed doesn't continue to grow over
multiple iterations of
while 1:
x.delete(1.0, end)
x.insert(end, s)
ps()

While the roughly-equivalent tcl script doesn't have quite the same memory
usage, you'll notice that no storage is freed after '.t delete 1.0 end'.
I suspect that this means some internal structure of the text widget
never shrinks when its contents shrink. Add to that the need to keep a
copy of the Python string converted to TCL, and the memory usage may be
"explained" as a combination of a Tkinter limitation and a Tk
limitation.

Jeff

proc ps {} {
puts [exec ps u [pid]]
}

text .t
pack .t

set s [string repeat "[string repeat x 72]\n" 40960]
ps ;# RSS 5520

.t insert end $s
ps ;# RSS 10240

.t delete 1.0 end
ps ;# RSS 10248

.t insert end $s
ps ;# RSS 10248

.t delete 1.0 end
ps ;# RSS 10248

.t insert end ""
ps ;# RSS 10248

Edward K. Ream

unread,
Nov 26, 2002, 4:51:43 PM11/26/02
to
Does this version of the Tk Text widget support unlimited undo? If so, text
can not ever really be deleted and the widget will appear to leak. Not all
"permanent" uses of memory can be considered bad.

Edward
--------------------------------------------------------------------
Edward K. Ream email: edr...@tds.net
Leo: Literate Editor with Outlines
Leo: http://personalpages.tds.net/~edream/front.html
--------------------------------------------------------------------


David LeBlanc

unread,
Nov 26, 2002, 4:31:51 PM11/26/02
to
> -----Original Message-----
> From: python-l...@python.org
> [mailto:python-l...@python.org]On Behalf Of Jeff Epler
> Sent: Tuesday, November 26, 2002 10:04
> To: Maric MICHAUD
> Cc: Python-List (E-mail)
> Subject: Re: memory leaks in Tkinter
>
>
> I think the term "memory leak" is usually used when something can make
> the process use an unbounded amount of memory over multiple iterations.
> Here, the amount of memory consumed doesn't continue to grow over
> multiple iterations of
> while 1:
> x.delete(1.0, end)
> x.insert(end, s)
> ps()
>
> While the roughly-equivalent tcl script doesn't have quite the same memory
> usage, you'll notice that no storage is freed after '.t delete 1.0 end'.
> I suspect that this means some internal structure of the text widget
> never shrinks when its contents shrink. Add to that the need to keep a
> copy of the Python string converted to TCL, and the memory usage may be
> "explained" as a combination of a Tkinter limitation and a Tk
> limitation.
>
> Jeff
<snip>

I know of one tkinter application that uses a lot of text widgets and leaks
hugely per Jeff Eppler's definition. I haven't figured out what the leak is,
but it's _quite_ noticable and I wish it wasn't there since I like the app a
lot. The app is Leo ( http://sourceforge.net/projects/leo/).

David LeBlanc
Seattle, WA USA


David LeBlanc

unread,
Nov 26, 2002, 5:45:44 PM11/26/02
to
Edward;

This wasn't intended as a jibe at Leo. The memory leakage really does bug
me, and I think it's a programming error, not an underlying attribute or
flaw.

As we've discussed before, Leo chews up large chunks of memory when outlines
are expanded/collapsed, and even when the same sections of the outline are
expanded/collapsed, the memory usage grows by significant amounts. I don't
see how this relates to undo since there's no change in the text.

BTW, I think that the text widget in Tk 8.4 does support undo, but I'm not
sure since I'm not active in the Tcl area much anymore.

David LeBlanc
Seattle, WA USA

> -----Original Message-----
> From: python-l...@python.org
> [mailto:python-l...@python.org]On Behalf Of Edward K. Ream
> Sent: Tuesday, November 26, 2002 13:52
> To: pytho...@python.org
> Subject: Re: memory leaks in Tkinter
>
>

> --
> http://mail.python.org/mailman/listinfo/python-list


Edward K. Ream

unread,
Nov 26, 2002, 9:07:04 PM11/26/02
to
> This wasn't intended as a jibe at Leo. The memory leakage really does bug
> me, and I think it's a programming error, not an underlying attribute or
> flaw.

Not to worry. I welcome bug reports. However, you have the best chance of
getting my attention by reporting them on Leo's SF site. I just stumbled on
this thread because I've been looking into leaks myself recently, and I have
been wondering how to separate "real" leaks from data associated with undo.

> As we've discussed before, Leo chews up large chunks of memory when
outlines
> are expanded/collapsed, and even when the same sections of the outline are
> expanded/collapsed, the memory usage grows by significant amounts. I don't
> see how this relates to undo since there's no change in the text.

I don't remember this as a bug report. Probably just my memory :-) Anyway,
it's now on Leo's bug list. I know Idle has a comment in its tree code
about leaking bindings, and Leo uses similar code. I'm not sure what can be
done about it; I've looked at this code several times and inspiration never
seems to strike.

Leo 3.9 uses only about 5% of the memory previously used for undoing text
operations. I was thinking that text undo was the only significant memory
hog; apparently I was wrong. Thanks again for the bug report.

0 new messages