@colouredfunk You did not write what version of Away3D (Full/Lite, F9/
F10) you were using, but based on your code I guessed it was the Flash
9 branch.

I've had a look at your file using the Flash Builder profiler and I
get the same results but this is not a memory leak in Away3D. Your
main problem here is that you add and remove the view all the time.
You should not need to do this ever. One view should be enough for any
object as you can change what scene it is rendering.

In the example below, I've changed your code so that the view is only
created once and that on every second frame we make 500 planes and
render them so we see some faster results. On the next frame we remove
them.

Pay close attention to your profiler as you run this code. Initially,
a lot of objects will accumulate and it will look like there is a
memory leak. However - after creating some ten thousand planes, the
memory usage flattens out. It's only at this point that the Garbage
Collection kicks in.

J

CODE:

package
{
        import away3d.containers.View3D;
        import away3d.materials.WireColorMaterial;
        import away3d.primitives.Plane;

        import flash.display.Sprite;
        import flash.events.Event;
        import flash.events.MouseEvent;

        [SWF(backgroundColor="#000000", width="800", height="600")]
        public class Leak3 extends Sprite
        {
                private var _view:View3D;
                private var _clickFlag:Boolean = false
                private var numPlanesPerFrame:Number = 500;
                private var planeArray:Array;

                public function Leak3()
                {
                        planeArray = new Array();
                        _view = new View3D();
                        addChild(_view);
                        _view.x = 400;
                        _view.y = 300;
                        this.stage.addEventListener(Event.ENTER_FRAME, 
_onMouseClick);
                }
                private function _onMouseClick(e:Event):void
                {
                        if (_clickFlag) {
                                cleanUp();
                        } else {
                                setUp();
                                _onEnterFrame(e);
                        }
                }
                private function cleanUp():void
                {
                        var plane:Plane;
                        while(planeArray.length > 0){
                                plane = planeArray.pop() as Plane;
                                _view.scene.removeChild(plane);
                        }
                        _clickFlag      = false;

                }
                private function setUp():void
                {
                        var i:int; var plane:Plane;
                        for(i = 0; i<numPlanesPerFrame;i++){
                                _clickFlag = true;
                                plane = new Plane();
                                plane.width = 80;
                                plane.height = 60;
                                plane.x = 800*Math.random()-400;
                                plane.y = 600*Math.random()-300;
                                plane.bothsides = true;
                                plane.material = new WireColorMaterial();
                                _view.scene.addChild(plane);
                                planeArray.push( plane );
                        }
                }
                private function _onEnterFrame(e:Event):void
                {
                        _view.render();
                }
        }
}

Reply via email to