Re: reuse memory and unset seems slo
From: miguel sofer (msofer_at_users.sf.net)
Date: 01/11/05
- Previous message: Bob: "Action-oriented vs Object-oriented"
- In reply to: mitch: "reuse memory and unset seems slo"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 11 Jan 2005 16:16:15 -0300
mitch wrote:
>
> so to reuse memory, do I have to reassign new data to original name
> assigned to the old listvar ?
>
>
> or could I do something like:
>
>
> set newlist_1 [ list 1 2 3 4 5 6 7 ... 999999 ]
> unset newlist_1
>
> set newlist_2 [ list 1 2 3 4 5 6 7 ... 999999 ]
>
>
>
> will that reuse the memory?
Yes. Depending en exactly what you are trying to do, there may be better
ways.
>
> Does it go into sort of a common holding place called
> "available memory for any variable in this program "
We tend to call it "pool (or linked list) of free Tcl_Objs".
>
>
> and a side note about something I noticed, is it takes almost as long to
> do a
> "unset varname" as it did to load it up. When there are millions of
> list elements the time is surprizingly long.
Yes. What is happening internally is:
(1) every list element gets its reference count decreased, and the
Tcl_Obj is returned to the pool if the refCount is now zero
(2) the memory for the array of pointers is returned to the OS
Depending on details, you may help the core omit step (2) (and the
immediate alloc afterwards) if you are about to define a new list, by
modifying in-place.
>
> In this same newsgroup someone tried to explain that to me a year or so
> ago, but I still don't understand that. doesn't one pointer also point
> to the next one ? So if you drop the first one you loose them all ?
Nope - tcl lists are not implemented as linked lists, they are c-arrays.
That is why they are so fast (constant time) to access by index.
Note that even if it was a linked list, "drop the first and loose them
all" sounds like a recipe for a very massive memory leak (except maybe
in garbage collected environments).
- Previous message: Bob: "Action-oriented vs Object-oriented"
- In reply to: mitch: "reuse memory and unset seems slo"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|