Curious question:
Take this function:
function something():Array
{
var ar:Array = [];
var i:MovieClip;
for each(i in list)
ar[ar.length] = i.x;
return ar;
}
Will ar be discarded, or does it continue to chill in my memory, adding to the memory being used each time I call this function?
My question applies to each of these cases:
var val:Array = something();(obviouslyvalis stored in memory, but what about the original array created in the function?)something();
Would it maybe be safer to always do this instead?:
return ar.slice();
Garbage Collection is done automatically by the flash engine. However it is not done instantaneously. In flash, anything that is not referenced will be GC’ed.
[See http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html for more details]
So for your function case of ‘something()’
Your 100 or so arrays generated will ‘chill’ for probably a few ms (varies) before getting cleared up by GC. However…
Your 100 or so arrays WILL STAY, as long as the variable ‘stupidArr’ exists. However if this occurs next.
OR
As long as the array in your function ‘something()’ is not refrenced (cleared with each pop, or complete null). By a variable accessible to you. The item will be free’d for GC. And hence will leave the memory.
So “Array.slice()” for your function is actually a bad idea, cause it doubles the memory consumption before GC (affects performance)
On another note, if lets say your function has useless variables, for eg: Your loop counters. These too are GC’ed if they are not refrenced at the end of the day. For flash, hence variable and garbage collection is ‘pretty easy’. Just know this rule of thumb.