Rebol[
    Title: "Mem-Usage - a set of routines to test memory usage"
    File: %memusage.r
    Email: rotenca@libero.it
    Author: "Romano Paolo Tenca"
    Version: 0.0.6
    Date: 13/11/01
    History: [
        0.0.6 "Cleaned some code"
        0.0.5 "Removed init-mem"
        0.0.4 "Added recycle/off"
        0.0.3 "Use an object to store old/new/pool-usg - no load with do"
        0.0.2 "First public release"
    ]
    Category: [2 util]
    Purpose: {
        Functions to test memory usage:

        pool: "Return the total memory in use in the allocated pool"
        mem-free: "Return the total free memory in the allocated pool"
        mem-ld: return memory usage of "load string"
        mem-tb: return memory usage of "to-block string"
        mem-do: return memory usage of "do string"
        mem-all: call mem-ld and mem-do and return the difference
        mem-func: memory usage of loading the source string code and creating a function
        
        Examples:
            a: 1
            mem-all ""          ; memory usage of a empty block
            mem-all "a"         ; memory usage of a block with a word
            mem-all "func [][]" ; memory usage of a new void func
            mem-all "make object! []"   ; memory usage of a void object
            mem-all %prova.r    ; memory usage of loading and executing a rebol program
            mem-do "help 4"     ; memory usage of executing some code
            mem-func :help      ; memory usage of re-loading and re-creating a function
            print pool          ; memory used
            print mem-free      ; memory free
    }

]
pool: func ["Return the total used memory in the allocated pool" /local tot-used][
    tot-used: 0
    foreach y system/stats/pools [
        if 1 <> first y [
             tot-used: y/2 - y/3 * y/1 + tot-used
        ]
    ]
    tot-used
]
mem-free: func ["Return the total free memory in the allocated pool"] [system/stats - pool]
mem-tb: func [{Return memory usage of "to-block string/file"} code [string! file!]][
    recycle
    recycle/off
    mem-ob/old: pool
    to-block code
    mem-ob/new: pool
    recycle/on
    print ["To block  :" mem-ob/new - mem-ob/old - mem-ob/pool-usg]
    mem-ob/new - mem-ob/old - mem-ob/pool-usg
]
mem-ld: func [{Return memory usage of "load string/file"} code [string! file!]][
    recycle
    recycle/off
    mem-ob/old: pool
    load code
    mem-ob/new: pool
    recycle/on
    print ["Load      :" mem-ob/new - mem-ob/old - mem-ob/pool-usg]
    mem-ob/new - mem-ob/old - mem-ob/pool-usg
]
mem-do: func [{Return memory usage of "do string/file"} code [string! file!]][
    recycle
    recycle/off
    mem-ob/old: pool
    do code
    mem-ob/new: pool
    recycle/on
    print ["Execute   :" mem-ob/new - mem-ob/old - mem-ob/pool-usg]
    mem-ob/new - mem-ob/old - mem-ob/pool-usg
]
mem-func: func [
    {Return difference between "load the function code" and "load and create the function code"}
    code [function!]
][
    - (mem-ld mold :code) + (mem-do mold :code)
]
mem-all:  func [
    {Return difference between "load string/file" and "do load string/file"} code [string! file!]
][
    - (mem-ld :code) + (mem-do :code)
]
mem-ob: context [recycle/off old: pool new: pool recycle/on pool-usg: new - old]
;change 'comment in 'do to try all examples
comment [
    a: 1
    mem-all ""          ; memory usage of a empty block
    mem-all "a"         ; memory usage of a block with a word
    mem-all "func [][]" ; memory usage of a new void func
    mem-all "make object! []"   ; memory usage of a void object
    ;mem-all %prova.r   ; memory usage of a rebol program
    mem-do "help 4"     ; memory usage of executing some code
    mem-func :help      ; memory usage of re-loading and re-creating a function
    print pool          ; memory used
    print mem-free      ; memory free
    recycle
    print pool          ; memory used
    print mem-free      ; memory free
]
;this can be removed
if not value? 'my_local_user [
    print system/script/header/Purpose
    ask "See the source for examples - Return to Quit - Esc for Shell "
]