AS3 Garbage Collector Note

I made a custom class that extended Sprite, and created an instance. Then I deleted it, but lo and behold, it was still there. After searching live docs and the web for some kind of destroy function or something else, I figured I’d ask one of the smartest humans I know personally, Darron Schall. Of course, he had the answer right there.

In AS2, when you delete an object, the player pretty much grabs it, carries it to the incinerator, shoves it in and watches to make sure it’s completely destroyed. You delete it and it’s gone.

On the other hand, when you delete an AS3 object, it gets put out on the sidewalk in a big green bag for later pickup. Eventually, the garbage collector will come along and take it away. Maybe in the next couple of milliseconds, maybe some time later in the week. If you look, you might still see it there, but it’s not really polite to keep staring at it.

I guess it’s just one of those things that at first kind of bugs you, because you know it’s sitting there, and you want it to be actually gone. But I guess after a while, you have faith that the GC really does take care of it in good time, and you start to relax.

I can’t claim to know much about that stuff, but apparently this is much better, more efficient, etc. Fine with me, now that I know what’s happening. Anyway, just wanted to share that in case anyone else runs into the same thing.

This entry was posted in Flash. Bookmark the permalink.

26 Responses to AS3 Garbage Collector Note

  1. JesterXL says:

    I believe this is the new GC they put into Flash Player 8, and Flash Player 9 uses it as well.

  2. Josh Tynjala says:

    They seem to have made some changes to the GC in Flash Player 9 too. I have a SWF that plays fast in player 7, chokes hard in player 8 under certain situations, then as of player 9 beta 2, it’s speedy again. It’s a pain because everyone is either using 8, or they’re going to be soon. We can’t force people to downgrade to 7, and it’s too early to recommend 9. I’ve been trying to figure out where exactly the GC is having trouble for the last couple weeks, but it’s nothing obvious.

  3. kp says:

    I think it’s different though.

    var o:Object = new Object();
    trace(o);
    delete o;
    trace(o);

    In AS2 Flash 8 I get:
    [object Object]
    null

    In AS3 I get:
    [object Object]
    [object Object]

  4. Ben Stucki says:

    This sounds similar to how .NET handles garbage collection. I think the theory is that when the runtime needs more memory it will find objects waiting for collection and destroy them, but until then it’s not wasting resources on garbage collection that isn’t required.

  5. Brent Bonet says:

    So I’m guessing if you really want it gone you should set the var to null?

    Cheers,
    Brent Bonet
    DarbyMedia
    IQ Interactive

  6. Keith,

    I’d log that as a bug. Even if the object doesn’t get GC’ed the variable “o” should return undefined because the reference should have been deleted immediately. Whether the GC then cleans up the actual object in memory is dependent on it’s reference count (and depending on its scope, mark sweeping).

    A deleted variable should never return anything.

    FYI – it sounds like Adobe has done some tweaking to the GC in fp9, but the big GC rewrite was part of fp8, which included support for mark sweeping (which rocks because it catches circular references).

    Good catch!

  7. kp says:

    Grant, that makes sense. I logged it.

  8. kp says:

    Actually, the more I think about it, the more it seems like a bug. How often do you see code that says something like:

    if(something == undefined)
    {
    create something
    }
    do action with something

    If something is not undefined, but is actually marked for deletion, this could create some serious havok in a program.

  9. tomsamson says:

    yeah,totally.
    with “i logged it” did you mean you posted it to the adobe guys as bug feedback?
    This is definately something which should be fixed.

  10. Michael says:

    In Actionscript 3.0 the delete operator does not work the same as in Actionscript 2. You remove references to objects by simply setting the reference to null.

    so:

    var o:Object = new Object();
    trace(o);
    o = null;
    trace(o);

    will return

    [object Object]
    null

    Or you can alternatively set it to undefined.

    -Michael

  11. Michael says:

    http://www.kirupa.com/forum/showpost.php?p=1884719&postcount=35

    Smart Guy, that Senocular is… 😉

    Take care, sorry for posting twice.

    (by the way, big fan Keith :))

  12. mitch says:

    i’m using cubicVR from andre michelle, and if i want to delete cubicVR app, i can’t, not even if i set to null all reference, is there a way to “destroy” it ? i can remove it as diplaycontainer as well, but VR will keep on run…how can be added that VR app and then deleted from main application ?

  13. Andrew says:

    I have this extact same problem, I load one swf into another then want to unload it when I’m finished so that it completely stops running and can be called to be added again later. Using Loader.unload and .removeChild doesn’t stop the swf from running.

  14. phil says:

    yeah – very bad problem
    plus in that case the removed SWF continues running and waste’s a lot of system resources that could be freed up up if the so called “more efficient” GC would just throw it out

    as2 garbage collector seems a lot more efficient to me

  15. kp says:

    It’s not a bad problem. It just requires that you keep track of what you have running and remove your timers / enterFrames when you don’t need them. A bit more responsibility, but that comes with a more powerful language.

  16. phil says:

    I haven’t seen anything that AS3 can do that AS2 can’t

  17. kp says:

    phil, it’s not so much that AS3 can do things AS2 can’t. You can accomplish most things in most languages one way or another. I’d say some of the more complex things are easier in AS3, while some simpler things are more complex in AS3. AS3 is also waaaaay faster than AS2.

  18. phil says:

    Correct me if Im wrong because I don’t actually know – but isn’t Flex supposed to be used for those more complex apps?
    I always thought that Flash was supposed to be a design/GUI based program – which allowed designers to make their designs interactive. Now it seems like more of a code based program than a design based one.
    Should designers be learning something new – like Silverlight?

    I consider myself to be sort of a designer and a coder as I can do PHP, Java, and even AS3 – and eventually I’m sure I’ll fall into line on AS3 – but the thing that turned me on to Flash when I started using it years ago was that you could accomplish a lot of great things without needing a lot of code – like using Windows or OSx instead of Dos or Linux.

    Seems like maybe Adobe lost the vision that Macromedia had.

  19. Swav says:

    Hi,

    I have created a test movie loaded by another movie to watch how GC deals with that. And it works, but only when you run swf independently (as swf, exe or hosted on the html page). GC doesn’t work if you test movies in Flash (Ctrl+Enter). Movies has to be cleaned from eventListeners, all streams and timelines has to be stopped before unload() takes place.

    This manual stopping/removing is very weak solution in my opinion. It limits you to load the swfs that comply with your internal structure (so you can easily dispose all streams and eventListeners).

    And what if I would like to build eLearning player which loads assets made by third party providers? I have to force them to stick with one structure and I am sure this will generate loads of bugs during production.

  20. Phil says:

    I have a video gallery – all the videos play in the same player – I just change the source URL of the FLV

    However when I remove the instance of the loaded “videos” swf the videos continue playing – even when I disconnect everything

  21. Phil says:

    the audio of the video keeps playing that is….the video itself is obviously removed from the stage

    and yes this is STILL a problem I’m battling with…I love AS3!!

  22. hashi says:

    use SoundMixer.StopAll();

    it worked for me.. i’ve a video gallery too and im tryin to the unload the video playing before the next one loaded.. removing child or unload doesnt seem to work.. although they r removed from stage they r still in memory and the next one take ages to load.. if anyone know the AS3 unloading mechanism please share it..

    Thanks

  23. chad says:

    I just battled with an app for a couple days and the problem was caused due to the change in garbage collection 🙁

    I was creating an object in flex inside of a method… once the method finished the netstream object would continue to stream, sometimes even multiple instances, but once I added more than a couple the GC would kick in and delete the functioning Object references from memory and all of my streams would stop simultaneously. This was happening because the vars created inside of the method were flagged for GC once the method was done running, but a functional reference would say in memory for minutes or even hours if the app wasn’t doing anything more than using existing Objects from memory. Needless to say storing a reference to the object in an array (this.live_streams[0]) fixed the bug.

    Side note… found the fix on accident lol.

  24. alex says:

    does any one know for AS 3.0 if there is a way to clear one .swf after loading a new one? cuz i am hitting almost 4g of PFusage and it slows the computer right down. i am new so a code snippet would be greatly appreciated.

  25. encoder says:

    if you trace the object in yr code, there is no way that you can delete it. remove any events from it and remove it from the stage.

    w8 and there will be no more.

    in theory flash deletes any objects that are not used anymore in the code nor the stage. the only way to test this if you observe the memory usage eventually with a huge bitmapData.

    should work only with dynamic objects.

  26. dbam says:

    just a remark on Michael’s post…

    Although You can set a ‘write’ property of an object to null to delete it (mark it as garbage), You can’t assign a value (not even null) on a property marked ‘read-only’.
    (As this happens to be the case with AVM1Movie.content)
    I’m having hard time with loading/unloading that type of content in AS3.

Leave a Reply