memory leaks in Tkinter

Jeff Epler jepler at unpythonic.net
Tue Nov 26 13:04:01 EST 2002


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




More information about the Python-list mailing list