2

I'm making a rather large flash project and so I'm concerned about memory usage. At the end of each section of the application I remove the overarching parent element that holds the content. Although this remove the parent, does this also free up the memory for each of the children contained within this, or should I run an iteration to remove those prior to removing the parent?

I'll give a little more explanation in-case I'm not expressing what I want:

addChild(movie1);
movie1.addChild(movie2);
movie1.addChild(movie3);

By using this code:

removeChild(movie1);

Does it remove movie2 and movie3 from memory or are they still stored, just unlinked?

Dan Hanly
  • 7,829
  • 13
  • 73
  • 134

3 Answers3

3

If movie2 and movie3 aren't referenced anymore by another object, they should be garbage collected. The same applies for movie1.

From Creating visual Flex components in ActionScript :

To programmatically remove a control, you can use the removeChild() or removeChildAt() methods. You can also use the removeAllChildren() method to remove all child controls from a container. Calling these methods does not actually delete the objects. If you do not have any other references to the child, Flash Player includes it in garbage collection at some future point. But if you stored a reference to that child on some object, the child is not removed from memory.

splash
  • 13,037
  • 1
  • 44
  • 67
  • I agree, and highly recommend downloading and watching the presentation you can find here : http://blogs.adobe.com/aharui/2007/03/garbage_collection_and_memory.html. It is a bit old but explains garbage collection mechanism very well. – David Sep 17 '10 at 10:00
1

Well, nothing is removed from memory so far , it really depends on how movie1 is referenced. Let's say that later in the application you want to add movie1 again, you should find that movie1 is not only still here but it also contains movie2 & movie3.

var movie1:MovieClip;

whatever();
anotherMethod();

function whatever():void
{
    movie1 = new MovieClip();
    var movie2:MovieClip = new MovieClip();
    var movie3:MovieClip = new MovieClip();
    movie1.addChild(movie2);
    movie1.addChild(movie3);
    addChild(movie1);

    whateverElse();
}

function whateverElse():void
{ 
    //here, nothing is garbage collected, movie1 exists outside
    //the scope of this function
    removeChild(movie1);

    //now you can
    tryThis();

    //but if you do this you're effectively killing movie2 & movie3 , since
    //they're not referenced anywhere else.
    removeChild(movie1);
    movie1 = null;

    //this is going to throw an error , movie1's gone!
    tryThis();
}

function tryThis():void
{
    //yes ,it's still here with all its content
    addChild(movie1);
}

function anotherMethod():void
{
    var movieN:MovieClip = new MovieClip();
    var movie2:MovieClip = new MovieClip();
    var movie3:MovieClip = new MovieClip();
    movieN.addChild(movie2);
    movieN.addChild(movie3);

    // do all you need to do... 
    // then
    removeChild( movieN);
    getOut();
}

function getOut():void
{
   //life goes on without movieN, movie2 & movie3
   //waiting to be garbage collected
}
PatrickS
  • 9,539
  • 2
  • 27
  • 31
  • So the answer is that yes, if I want to remove it completely from Memory, I have to remove all child elements and THEN it's parent element to keep the application tight – Dan Hanly Sep 18 '10 at 00:27
  • 1
    if movie2 & movie3 are not referenced by any other object , removing movie1 will be sufficient. look at the code! in anotherMethod() i only remove movieN & in getOut() movieN is not referenced anymore, so the three movieclips will be garbage collected – PatrickS Sep 18 '10 at 02:01
0

Ofcourse it removes all of the 3.

Child movie 1 has two children, movie2 + 3. If he dies because of some Instance decease the children will possibily die too.

Maybe I'm wrong but you can also try:

trace(movie2);
  • It's not of necessity that there are removed, because they could always be referenced by another object. – splash Sep 17 '10 at 09:53
  • I don't think performing `trace(movie2);` is relevant as you don't know when the garbage collector will run and "delete" movie2. – David Sep 17 '10 at 09:54