« Return to Thread: Object deletion and Memory Issue

[SOLVED] TriangleMesh3D does not cleanly dispose - Memory leaking

by tPS :: Rate this Message:

Reply to Author | View in Thread

So finally i worked through the code and found the solution.


the memory leaking is caused by the Materials registerObject function.
the Material stores a reference to the DisplayObject3D's instance in a dictionary, so you manually have to unregister the object and then the object as well as the material are ready for the garbage collection.

i would propose to add a dispose funciton to the DisplayObject3D Class

/**
 * cleans up for deletion
 *
 * added by tPS
 */
public function dispose():void{
        if(_material){
                _material.unregisterObject(this);
        }
}

happy flashing



Hello,

after further study of the code i was able to isolate the cause of the issue.

The problem lies somewhere in the instanciation of the TriangleMesh3D Class.

if you try the following example you will notice the application will constantly need more and more memory. if you comment the blocks in function addCube and decomment the respected line to change TriangleMesh3D into Vertices3D everything works like expected.




package memoryTest
{
        import com.tPS.utils.FPSMemCounter;
       
        import flash.events.Event;
        import flash.events.TimerEvent;
        import flash.utils.Timer;
       
        import org.papervision3d.core.geom.TriangleMesh3D;
        import org.papervision3d.core.geom.Vertices3D;
        import org.papervision3d.view.BasicView;
       

        public class View extends BasicView
        {
               
                private var _cubes:Array;
               
                public function View()
                {
                        super();
                        addChild( new MEMCounter() );
                        addEventListener( Event.ADDED_TO_STAGE, setup );
                }
               
               
                private function setup( e:Event = null ) : void {
                       
                        _cubes = [];
                       
                        createCubes();
                       
                        stage.addEventListener( Event.ENTER_FRAME, render );
                        var t:Timer = new Timer( 500 );
                        t.addEventListener( TimerEvent.TIMER, refreshCubes );
                        t.start();
                }
               
               
                private function createCubes() : void {
                        var i:int = -1;
                        while( ++i < 1000 ) {
                                addCube();
                        }
                }
               
               
                private function addCube() : void {
                       
                        //// MEMORY LOSS //////////////////////////////////////////////////////////////////////////
                        var c:TriangleMesh3D = new TriangleMesh3D( null, [], [] );
                        ///////////////////////////////////////////////////////////////////////////////////////////////
                       
                        /*
                         * NO MEMORY LOSS
                         */
                        //var c:Vertices3D = new Vertices3D( [] );
                       
                       
                        _cubes.push( c );
                }
               
                private function refreshCubes( e:Event ) : void {
                        var i:int = _cubes.length;
                        while( --i > -1 ) {
                                var c:Vertices3D = _cubes[i] as TriangleMesh3D;
                                //scene.removeChild( c );
                                delete _cubes[i];
                        }
                        _cubes.splice(0);
                        _cubes = [];
                        createCubes();
                }
               
               
               
               
                private function render( e:Event = null ) : void {
                        renderer.renderScene( scene, _camera, viewport, false );
                }
               
        }
}


import flash.display.Sprite;
import flash.text.TextField;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.system.System;
       
class MEMCounter extends Sprite{
       
        private var _txt:TextField;
       
        public function MEMCounter() {
                addEventListener( Event.ADDED_TO_STAGE, setup );
        }
       
        private function setup( e:Event ) : void {
                _txt = new TextField();
                _txt.width = 120;
                addChild( _txt );
               
                var t:Timer = new Timer( 1000 );
                t.addEventListener( TimerEvent.TIMER, updateMem );
                t.start();
        }
       
       
        private function updateMem( e:TimerEvent ) : void {
                _txt.text = Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 ) + 'Mb';
        }
}






tPS wrote:
Hello Everyone,

i have run across a problem regarding to the fact that i can't delete objects in Papervision3d.

i made a simple example that showcases the problem.
it simply creates 10 cube objects every 10 seconds. if there are already some cubes created, it tries to delete them.
as you can see on the memory monitor the objects still remain in memory.

http://www.perfectscript.de/memoryleaking/index.html

i have included the code.
sources.zip

 « Return to Thread: Object deletion and Memory Issue