Version 0 of Fixing Tk_Uid leaks

Updated 2007-10-09 08:27:21 by suchenwi

Richard Suchenwirth 2007-10-09 Why does Tcl leak memory? As explained on that page, Tk_Uids are created for text and canvas tags and never released. In a long running process, which on a canvas displays images and bounding boxes (polygons), I saw memory rise from 65MB to over 400 MB after some hours. Fixes as suggested by dkf:

  • reuse the displayed image: don't do each time
 $c create image 0 0 -image $im -anchor nw
 ...
 $c delete all

but create and display the image once, and reconfigure it with

 $im copy $im2 -shrink
  • reuse polygons by caching them in a global array g:
        if {![info exists g(of$n)]} {
            set id [$w create poly $coords -fill {} -outline $color -tag "xx of$n"]
            set g(of$n) $id
        } else {
            set id $g(of$n)
            $w coords $id $coords
            $w itemconfigure $id -outline $color
            $w raise $id
        }

and instead of deleting them, just move them "out of sight"

   foreach i [$w find withtag xx] {$w coords $i -100 -100 -99 -99}

Category performance