releasing a list

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
Bushmills
Posts: 286
Joined: 23 Sep 2014 21:56

releasing a list

Post by Bushmills » 31 Oct 2014 14:27

Will lists, not referenced any longer, be released when I remove the variable the list pointer has been assigned to?
Specifically, I am using/toying with some code, using nested lists for intermediate storage.
It looks, for example, like this:

Code: Select all

// intermediate list of lists. Meant for avoiding long sequences of addElement(mem,...)
// There's a reason why I don't write this as mem=newList(<list_of_all_items_in_nested_lists>);
app=newList(
   newList(here,over,comma,swap,store),
   newList(here,over,allot,swap,cmove),
   newList(...),
   newList(bye)
);

// now looping through nested list "app", copying references to contents to list mem:
for (words in app) {                               // loop through lists
   for (word in words)  {                         // loop through list items
      addElement(mem,word)
   }
}

// nested lists "words" not needed any longer. Release?
removeVariable(app);
After that "for (words in app)" loop i don't need that nested list any longer, and want to release it. It may be re-created again elsewhere in the script. Is what I'm trying here enough for doing so? I'm also wondering whether I should bother at all, given that intermediate list items, the beef of the list, still float around in memory, by re-referencing them from the mem list (as intended), and there may be little to gain. Main consideration was memory usage, as those nested lists can grow to non-neglectable number of items (several thousand to ten thousands).

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: releasing a list

Post by Martin » 31 Oct 2014 15:56

Lists can be garbage collected when not referenced anymore. Local variables are kept in memory as long as the flow is running but if you assign a new value to the same local variable or remove the local variable, the previous value can be garbage collected (when it's not referenced by another variable).

For example in this script:
list1 = newList(lots of elements);
list1 = newList();

The list that was assigned to the variable in the first line can be collected when the second line has been executed. There's no guarantee at what point in time this really happens since it's up to the VM that runs Automagic.

In your example the list assigned to variable app can be garbage collected as soon as the line removeVariable(app); is executed. It probably makes sense to do this only when actions within the flow are looping and local variables are available for a long time. There's no need to remove the variable when the flow ends soon and the local variables are released automatically anyway.

You will probably get performance issues when using lists with thousands of objects. For complicated scripting it might be better to use something like android-scripting or even write your own plugin so that your script is not interpreted by Automagic but is directly executed by Dalvik or ART.

User avatar
Bushmills
Posts: 286
Joined: 23 Sep 2014 21:56

Re: releasing a list

Post by Bushmills » 31 Oct 2014 17:20

Thank you,, that makes it enitirely clear.
As for performance, or alternative implementation - I tend to write the kind of code I am trying to subject Automagic to in assembly language. But it gives me some weird kind of pleasure to also do implementation in less likely languages, like perl, javascript, editor macros, and even bash. My brainfuck implementation was postponed when I reached the point of having conceptualised random memory access :) Performance isn't therefore of utter importance with this.
The code belongs to an incremental compiler and interactive interpreter, which I tend to implement whenever I want to deepen my skills with any programming or scripting language. Using the same exercise allows me to do a more objective comparison between languages. So when I started dabbling with Automagic scripts, of course the same project came up again. It helps me learning about niceties and shortcomings of the language I'm toying with, as I sometimes need to find different solutions for the same problem. How much a language resists gives me an inkling of its stubbornness. Example observation: Automagic doesn't seem to supply bitwise logical operators, which puts it into the lua category. For a full implemenation (albeit doubtful I ever get there - character I/O is another problem I perceive) I will need bitwise logic. therefore I'd need to work around that.

For reference, the perl implementation is at http://scarydevilmonastery.net/perlforth,
javascript version (running in your browser) at http://forthfreak.net/jsforth80x25.html,
with source code at http://forthfreak.net/jsforth.html,
bash version can be found there too if interested sufficiently.

(edit) re: character I/O - I can probably use an Automagic widget as simulated screen for output. That should solve half of the problem already.

Post Reply