Version 0 of namespace size

Updated 2005-11-16 14:28:06 by suchenwi

Richard Suchenwirth 2005-11-16 - Hunting for leaks in our software, I could not use the TCL_MEM_DEBUG mechanism. But the following code gives an approximate size in bytes consumed by the variables and children of a Tcl namespace (of which ::, the global namespace, is of particular interest - all its (grand)*children are also added). If you call this proc repeatedly, you can observe whether data are piling up:

 proc namespace'size ns {
   set sum [expr wide(0)]
   foreach var [info vars ${ns}::*] {
       upvar #0 $var v
       if {[array exists v]} {
           incr sum [string bytelength [array get v]]
       } else {
           incr sum [string bytelength $v]
       }
   }
   foreach child [namespace children $ns] {
       incr sum [namespace'size $child]
   }
   set sum
 }

Usage example:

 % puts [namespace'size ::]
 179914

Category Debugging - Arts and crafts of Tcl-Tk programming