views:

94

answers:

2

Hi everyone,

I have a problem with my flash application because after a while that it is running, it eventually starts to slow down. My application involves something that needs to be replicated with the addChild() method. I've read some info on the internet which states that the cause of the slowing down or the lag in the application is that the removeChild() does not remove the child from the memory.

Are there any ways on how I can remove the child from the memory too? Any inputs are appreciated. Thanks.

+1  A: 

Check out this 3-part article on resource management in AS3 by Grant Skinner.

heavilyinvolved
+1  A: 

It looks like you are creating new Objects adding it to your stage and removing unwanted objects from the stage, which might lead to slow speed as there will be lots of unwanted object in the memory. In flash AS3, you can not totally rely on GC for garbage cleanup. So the best approach is to generate least possible amount of garbage and recycle unused objects whenever you need a new object.
For example, an application keeps putting some circle to the the stage and removing some of them at fixed time interval. So for this kind of resource implement a resource pool.

 public class ResourcePool {
  static function getCircle(prop:Object):Circle {
    //check if you already have some circle objects
    //if yes pick one apply the prop and return
    // else create a new circle with specified prop and return
  }
  static function recycle(circle:Circle):void {
    //add it to available resource array
  }
}
Now when you need a circle object then ask the ResourcePool for that:
 var c:Circle = ResourcePool.getCircle(someProperty);
And whenever you are removing a circle then recycle it properly so that it can be used later.
//remove circle1 object
ResourcePool.recycle(circle1);

bhups