How to unload a ByteArray using Actionscript 3?

前端 未结 8 2309
感动是毒
感动是毒 2020-12-30 18:32

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

I have tried the following:

// First non-working solution
byteA         


        
相关标签:
8条回答
  • 2020-12-30 18:35

    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();
    }
    
    0 讨论(0)
  • 2020-12-30 18:40

    Use these resources:

    http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b8cbfe-7ff7.html

    and this

    http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b8cbfe-7ff7.html

    0 讨论(0)
  • 2020-12-30 18:41

    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 20 MB, but it has 5 MB that is free for future allocations.

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

    0 讨论(0)
  • 2020-12-30 18:52

    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.

    0 讨论(0)
  • 2020-12-30 18:52

    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 blog posts that the GC in AVM2 is optimistic and will work its 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 overnight with some tests running on top of your app.

    0 讨论(0)
  • 2020-12-30 18:56

    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.

    0 讨论(0)
提交回复
热议问题