TCL Memory Release

I have a basic question regarding memory management in TCL.

  • Suppose I have a Tcl procedure. Inside the procedure, I declare an array and add some data to it. I do some crunches on this array. My question is, before the procedure returns, do I need to manually delete the array? Using:

    unset <array_name> 

    My answer is no. But I'm not sure, because I don’t know if there is a garbage collector in Tcl. Can someone comment on this, please.

  • Is using array in Tcl bad? I want to create an array of lists and, while reading the discussions, people said to use dict for this kind of thing, but since I have Tcl 8.4, I cannot. What is the problem with arrays in Tcl?

+4
source share
3 answers
  • Suppose I have a Tcl procedure. Inside the procedure, I declare an array and add some data to it. I do some crunches on this array. My question is, before the procedure returns, do I need to manually delete the array?

No. It will be deleted when the procedure returns (i.e., its duration is limited by the stack frame). You can unset it earlier if you want to free its memory, but you do not need to do this.

  • Is using array in Tcl bad? I want to create an array of lists and, while reading the discussions, people said to use dict for this kind of thing, but since I have Tcl 8.4, I cannot. What is the problem with arrays in Tcl?

Arrays are absolutely good at 8.4, if you remember that they are a collection that implements a map from arbitrary string keys to variables. Dictations are intended for cases when you need a value that contains a map from strings to arbitrary values. (There is a dict extension for 8.4 somewhere around.) You can use arrays to model matrices by choosing a character as a separator between subindexes (for example, a comma), and this method has been used by many people for a great effect.

However, it is more efficient to model them as lists of lists; lset and multi-index lindex allow you to effectively update and search for elements of the resulting matrix. (Although this is a bit verbose.) If you are dealing with a lot of data, improving efficiency can be very useful. (However, resolved matrices can be better executed as a Tcl array.) Also note that if you can upgrade to 8.5, the number of operations there will be much faster (in particular, including testing for the presence of an array element with info exists ).

+3
source

Tcl has garbage collection (the exact implementation is undefined, whether it refers to reference counting or some other method). No, you do not need to use unset to avoid memory leaks.

I'm not sure why arrays will be considered bad. You will need to provide links to such discussions in order to get specific comments.

+2
source

No, you do not need to free your local vars when you exit the procedure. They are automatically released. In most cases this happens, but there are exceptions (some extensions may require you to explicitly release things, and some packages, especially http, create tokens that need to be cleared)

If you create large arrays in the global namespace, they will not be freed, as they can still be accessed.

There is no problem using arrays in tcl, but make sure you use the right tool. In tcl, an array is an "associative array", that is, indexed by a string. If you need a c-style array indexed by an integer, a simple old list might be better (you can pass it by value), but you can take it a little awkwardly (subscriptip is a command, not a reference to a variable with a unit)

+1
source

Source: https://habr.com/ru/post/1390144/


All Articles