• memory usage

    From saito@saitology9@gmail.com to comp.lang.tcl on Thu Aug 8 16:21:21 2024
    From Newsgroup: comp.lang.tcl

    Sorry this may be quite basic but just wanted to confirm it:

    Let's say that I have a proc which generates a large dict and returns
    it. Then when used, the caller assigns it to a variable.

    How much memory is used, 10mb or 20mb? (let's ignore any extra memory
    needed to manage the dict structure and the proc calls).


    proc generate_10mb_dict {a b c} {
    # generate a large dict, in d

    return $d
    }

    set my_data [generate_10mb_dict $a $b $c]


    what is the ram use at this point?

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Rich@rich@example.invalid to comp.lang.tcl on Thu Aug 8 20:27:58 2024
    From Newsgroup: comp.lang.tcl

    saito <saitology9@gmail.com> wrote:
    Sorry this may be quite basic but just wanted to confirm it:

    Let's say that I have a proc which generates a large dict and returns
    it. Then when used, the caller assigns it to a variable.

    How much memory is used, 10mb or 20mb? (let's ignore any extra memory
    needed to manage the dict structure and the proc calls).


    proc generate_10mb_dict {a b c} {
    # generate a large dict, in d

    return $d
    }

    set my_data [generate_10mb_dict $a $b $c]


    what is the ram use at this point?

    Given your sample code, the usage is the amount taken up by the dict.

    If the dict is 10mb (as you suggest) then after "set my_data" the ram
    usage is 10mb.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From saito@saitology9@gmail.com to comp.lang.tcl on Thu Aug 8 19:39:27 2024
    From Newsgroup: comp.lang.tcl

    On 8/8/2024 4:27 PM, Rich wrote:

    Given your sample code, the usage is the amount taken up by the dict.

    If the dict is 10mb (as you suggest) then after "set my_data" the ram
    usage is 10mb.

    Thank you!
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Harald Oehlmann@wortkarg3@yahoo.com to comp.lang.tcl on Fri Aug 9 08:22:44 2024
    From Newsgroup: comp.lang.tcl

    Am 09.08.2024 um 01:39 schrieb saito:
    On 8/8/2024 4:27 PM, Rich wrote:

    Given your sample code, the usage is the amount taken up by the dict.

    If the dict is 10mb (as you suggest) then after "set my_data" the ram
    usage is 10mb.

    Thank you!

    Yes, TCL uses generally shared values with referent count.
    And each item is reference counted. And with TCL 9, even sublists are reference counted, e.g. stored only once.

    That means:

    set d [dict create a [string repeat a 100000]]

    d and a are reference counted.

    dict set d b [dict get $d a]

    does not copy the string, but reference it twice.

    A copy of the data is only done, if the data gets different:

    dict set d b "[dict get $d b]q"

    Now, dict item a and b are differently and thus b is copied.

    Anyway, it is endlessly complicated...

    Harald
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From saito@saitology9@gmail.com to comp.lang.tcl on Fri Aug 9 12:07:01 2024
    From Newsgroup: comp.lang.tcl

    On 8/9/2024 2:22 AM, Harald Oehlmann wrote:

    Now, dict item a and b are differently and thus b is copied.

    Anyway, it is endlessly complicated...


    :-)

    Thanks for the details. That makes sense.

    --- Synchronet 3.20a-Linux NewsLink 1.114