开发者

Are arrays defined within a function discarded at the end of the function in AS3?

开发者 https://www.devze.com 2023-03-18 12:32 出处:网络
Curious question: Take this function: function something():Array { var ar:Array = []; var i:MovieClip; for each(i in list)

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 qu开发者_高级运维estion applies to each of these cases:

  1. var val:Array = something(); (obviously val is stored in memory, but what about the original array created in the function?)
  2. 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()'

for(var i:int = 0; i<100; i++) {
    something();
}

Your 100 or so arrays generated will 'chill' for probably a few ms (varies) before getting cleared up by GC. However...

var stupidArr:Array = [];
for(var i:int = 0; i<100; i++) {
    stupidArr.push( something() );
}

Your 100 or so arrays WILL STAY, as long as the variable 'stupidArr' exists. However if this occurs next.

stupidArr = null;

OR

stupidArr.pop(); //Looped as desired

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.

If by any means possible, you can programically access the variable, it will persist. If you cant, it will be destroyed.


As far as I know var ar:Array = [] is local to the function something() and will be GC'ed when the function exits. Unless you pick it up when the function returns as you suggest in case 1: var val:Array = something();. Calling something(); should not accumulate memory.

Edit:

I found this link, while digging into this; check the PowerPoint.

If you only run that function once, the array will actually stay in memory. However, any allocation thereafter might trigger the Garbage Collector to run and it should eventually remove ar. Which still suggests that the variable ar will not, in the long run, hang around something() and clog your memory.

0

精彩评论

暂无评论...
验证码 换一张
取 消