if {[namespace which memory] eq ""} {
    error "Needs Tcl with \[memory] command enabled!"
}

set ::meminfo 0
proc meminfo {} {
    variable lastmeminfo
    dict incr lastmeminfo {bytes}
    set inf [memory info]
    foreach {_ k v} [regexp -all -inline -line {^(.*?)   *(\d+)$} $inf] {
        dict set mem $k $v
    }
    dict with mem {
        puts "total alloc = [expr {${current bytes allocated}}]"
        puts "step alloc = [expr {${current bytes allocated} - [dict get $lastmeminfo bytes]}]"
        puts "mallocs-frees = [expr {${total mallocs}-${total frees}}]"
        dict set lastmeminfo {bytes} ${current bytes allocated}
    }
}
