arrays - writemultibyte - vector actionscript 3




Unloading a ByteArray using Actionscript 3 (6)

So, if I load say 20MB from MySQL, in the Task Manager the RAM for the application goes up by about 25MB. Then when I close the connection and try to dispose the ByteArray, the RAM never frees up. However, if I use System.totalMemory, flash player shows that the memory is being released, which is not the case.

The player is "releasing" the memory. If you minimize the window and restore it you should see that the memeory is now much closer to what System.totalMemory shows.

You might also be interested in using FlexBuilder's profiling tools which can show you if you really have memory leaks.

How do I forcefully unload a ByteArray from memory using ActionScript 3?

I have tried the following:

// First non-working solution
byteArray.length = 0;
byteArray = new ByteArray();

// Second non-working solution
for ( var i:int=0; i < byteArray.length; i++ ) {
    byteArray[i] = null;
}

So, if I load say 20MB from MySQL, in the Task Manager the RAM for the application goes up by about 25MB. Then when I close the connection and try to dispose the ByteArray, the RAM never frees up. However, if I use System.totalMemory, flash player shows that the memory is being released, which is not the case.

Is the flash player doing something like Java and reserving heap space and not releasing it until the app quits?

Well yes and no, as you might have read from countless blogposts the GC in AVM2 is optimistic and will work it's own mysterious ways. So it does work a bit like Java and tries to reserve heap space, however if you let it long enough and start doing other operations that are consuming some significant memory it will free that previous space. You can see this using the profiler over night with some tests running on top of your app.


Have a look at this article

http://www.gskinner.com/blog/archives/2006/06/as3_resource_ma.html

IANA actionscript programmer, however the feeling I'm getting is that, because the garbage collector might not run when you want it to.

Hence http://www.craftymind.com/2008/04/09/kick-starting-the-garbage-collector-in-actionscript-3-with-air/

So I'd recommend trying out their collection code and see if it helps

private var gcCount:int;
private function startGCCycle():void{
    gcCount = 0;
    addEventListener(Event.ENTER_FRAME, doGC);
}
private function doGC(evt:Event):void{
    flash.system.System.gc();
    if(++gcCount > 1){
        removeEventListener(Event.ENTER_FRAME, doGC);
        setTimeout(lastGC, 40);
    }
}
private function lastGC():void{
    flash.system.System.gc();
}

I believe you have answered your own question...

System.totalMemory gives you the total amount of memory being "used", not allocated. It is accurate that your application may only be using 20mb, but it has 5mb that is free for future allocations.

I'm not sure if the Adobe docs would shed light on the way that it manages memory...


Unfortunately when it comes to memory management in Flash/actionscript there isn't a whole lot you can do. ActionScript was designed to be easy to use (so they didn't want people to have to worry about memory management)

The following is a workaround, instead of creating a ByteArray variable try this.

var byteObject:Object = new Object();

byteObject.byteArray = new ByteArray();

...

//Then when you are finished delete the variable from byteObject
delete byteObject.byteArray;

Where byteArray is a dynamic property of byteObject , you can free the memory that was allocated for it.






apache-flex