dcsimg
A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Accursed memory management

  1. #1
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749

    Accursed memory management

    In AS 2.0, removeMovieClip() would effectively wipe all memory for that clip and free it up instantly... so I could do massive image management tasks (scanning tiled 60000 pixel square images) with no problems.

    In AS 3.0, removeChild() only makes memory eligible for clearance... likewise BitmapData.dispose()... but garbage disposal never seems to happen so I am running up to 1 GB of memory for a single Flash movie and my machine is chugging!

    I can't find any reference on forcing garbage collection and (as far as I can make out) am clearing any pointers to these objects.

    Does anybody have any suggestions to clear memory more effectively?

    G

  2. #2
    FK Newb Beater
    Join Date
    Dec 2002
    Location
    Seattle
    Posts
    676
    There is no way to force Garbage collection but there are best coding practices that allow garbage collection to occur with more certainty.

    There has to be thousands of topics on this across all Flash boards including this one. Trouble seems to occur most frequently when listeners aren't removed or when references to objects persist in some way.
    Just because you changed the code, doesn't mean it's yours

    Most Recent Work:
    Commercial tanning beds website

  3. #3
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    In AS3 the display list is just a tree for _showing_ things...it has nothing to do with how your objects are scoped, stored, accessed, &c.

    Example: in AS2 I could say _root.attachMovie() and I've got a MC as a child of the root. In 3, I can say new MovieClip() and I've created a MC...but it isn't visible, it isnt even rendered anywhere, its just a variable in space. It happens to be a variable that has a visual component, but it's not defined by what it looks like or where it is in the tree, it's defined by the address in memory where it's stored. Make sense?

    To prepare something for GC, you need to eliminate all references to it, including listeners, variables, and it's place in the display list:

    PHP Code:
    var s:Sprite = new Sprite();
    s.addEventListener(MouseEvent.CLICKonClick);
    addChild(s); 
    Now my Sprite has 3 things tying it to memory, a var (s), a listener (click), and a place on the display tree (this.s). You need to clear all three of those before it can be collected:

    PHP Code:
    removeChild(s);
    s.removeEventListener(MouseEvent.CLICKonClick);
    undefined

  4. #4
    Actionscript Developer KigD's Avatar
    Join Date
    Jan 2003
    Location
    georgia
    Posts
    597
    Quote Originally Posted by Sleeve
    There is no way to force Garbage collection but there are best coding practices that allow garbage collection to occur with more certainty.

    There has to be thousands of topics on this across all Flash boards including this one. Trouble seems to occur most frequently when listeners aren't removed or when references to objects persist in some way.
    Technically you can force garbage collection with either the infamous gskinner hack (declaring two localconnections in a try catch) or with System.gc() which is available in 9.0.115. I've heard there are issues with System.gc() and that it just marks objects for sweep instead of actually removing them, but I'm not sure.
    K2xL - My games, tutorials, message boards, and experiments.
    Blog

  5. #5
    FK Newb Beater
    Join Date
    Dec 2002
    Location
    Seattle
    Posts
    676
    I thought the System.gc() is for the flash player debugger only therefore it will not work for all other versions. Is this not correct?

    Anyway, looking up gskinner, I found what may be a solution to the original problem in this post.
    Last edited by Sleeve; 04-24-2008 at 01:04 PM.
    Just because you changed the code, doesn't mean it's yours

    Most Recent Work:
    Commercial tanning beds website

  6. #6
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    While it is a bit of a pain in the ass, it is necessary that it is done this way.

    For example, when you attach a movieClip as an ENTER_FRAME listener you should be able to expect it to recieve these events even if it is not being displayed.

    If removeChild automatically freed the memory used by a displayObject, suddenly all events it was listeing for would no longer work and you would not be able to add it to another object.

    Because of this, you need to remove all listeners (Or use weak refrences if you want to dispose of the object after removal).

    I will admit that I think that events and garbage collection don't work very elegantly together in this case, but it is better than the alternative.

    I highly doubt that your problems are caused by the garbage collector not running. It is probbably running fine but all of your "removed" objects still have refrences to them.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  7. #7
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    If removeChild automatically freed the memory used by a displayObject, suddenly all events it was listeing for would no longer work
    This isn't my problem, my problem is that when I deliberately set out to destroy objects (I have implemented a cleanUp method that calls dispose() on every bitmap, removeEventListener on every listener and sets any references to an object to null... before removeChild()) the memory in use just keeps growing...

    In fact, I have no evidence of _anything_ being garbage collected at all.

    I highly doubt that your problems are caused by the garbage collector not running. It is probbably running fine but all of your "removed" objects still have refrences to them.
    It doesn't seem possible with the code I have, however, I'll keep digging as I can't believe that something so basic can stop me completing a project...

    It looks like I will need to get into reuse of objects to manage this, but that will be a non-trivial endeavour for what I am attempting.

  8. #8
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    ps: Sleeve, thanks for the reference, educational but depressing reading as it seems to confirm that I am up a creek with this one.

  9. #9
    Actionscript Developer KigD's Avatar
    Join Date
    Jan 2003
    Location
    georgia
    Posts
    597
    Quote Originally Posted by AlsoGaiusCoffey
    (as far as I can make out) am clearing any pointers to these objects.G
    You also may want to triple check this fact. If you remove all the listeners of an object that you created and remove all the references to the object or properties/methods of that object then that object should be collected.
    K2xL - My games, tutorials, message boards, and experiments.
    Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center