Updated 2012-11-18 00:19:22 by RLE

Phil Ehrens - UNIX only. This page was originally part of the Bag of algorithms.
 proc leakLogger { } {
      foreach { name pid pcpu pmem vsz rss } [ sysData ] { break }

      if { ! [ info exists ::memusage_t0 ] } {
         set     ::memusage_t0 [ clock seconds ]
         lappend ::memusage_t0 $vsz
         puts "inital size of $::argv0: $vsz Kb"
      }

      foreach { t0 x0 } $::memusage_t0 { break }
      set tn   [ clock seconds ]
      set xn $vsz
      set t    [ expr { $tn - $t0 } ]
      set leak [ expr { $xn - $x0 } ]
      if { $leak } {
         puts "LEAKED $leak Kb in $t seconds"
      }
      return {}
 }

 if {$tcl_platform(os) eq "SunOS" && $tcl_platform(osVersion) >= 5} {
    proc sysData {} {
       set data [exec /bin/ps -p [pid] -o "fname pid pcpu pmem vsz rss"]
       foreach datum [lrange [split $data "\n"] 1 end] {
          return $datum
       }
       puts stderr "Something's rotten in the state of Denmark..."
    }
 } else {
    proc sysData { } {
       set done 0
       set myname [ file tail $::argv0 ]
       set data [ exec ps -Ao "fname pid pcpu pmem vsz rss" ]
       set data [ lrange [ split $data "\n" ] 1 end ]
       foreach datum $data {
          foreach { name pid pcpu pmem vsz rss } $datum {
             if { [ regexp ^$name $myname ] || [ regexp ^$myname $name ] } {
                set done 1
                break
             }
          }
          if { $done } { break }
       }
       if { $done } {
          return $datum
       } else {
          puts stderr "dagnabbit, you're using that lame \"exec tclsh \"\$0\" \${1+\"\$@\"}\" thing, so this won't work!!"
       }
    } ;## -PSE
 }