A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: [AS3] memory leak in the new Loader class?

  1. #1
    Senior Member ozmic66's Avatar
    Join Date
    Oct 2005
    Posts
    472

    [AS3] memory leak in the new Loader class?

    I've been wrecking my brain over this problem for two days now, and I have found no solution yet. perhaps some of you actionscript experts here can help

    I am making an application where I load a picture from the web at a rate of once per second. When I load a new picture I unload the old one

    the problem is that the old pictures are never completely erased from memory and the application keeps on consuming more and more memory until I'm guessing it eats all the memory in the cache (I haven't run it for that long yet :))

    here is the code:
    what this does is load a picture when you click the screen, then unloads it when you click the picture. do it a few times and notice that the numbers (which show System.totalMemory) only go up but never go down to their original amount...

    You can copy and paste this code into flash 9

    Code:
    var url:String = "http://www.memorycity.com/shop/MBB/images/GIGABYTE_memory.jpg";
    
    stage.addEventListener(MouseEvent.CLICK,onclick)
    
    var txt:TextField = new TextField()
    txt.x=10
    txt.y=10
    txt.width = 400
    txt.height = 20
    
    addChild(txt)
    
    var txt2:TextField = new TextField()
    txt2.x=300
    txt2.y=10
    txt2.width = 400
    txt2.height = 20
    
    addChild(txt2)
    
    txt2.text = "click to load picture"
    
    function onclick(e:MouseEvent){
    	if(e.target==stage)
    	loadPic()
    }
    function loadPic(){
    	 var loader:Loader = new Loader();
    	loader.addEventListener(MouseEvent.CLICK, clickHandler);
    
    	var request:URLRequest = new URLRequest(url);
    	loader.load(request);
    
    	addChild(loader);
    	
    	//bring texts to front
    	removeChild(txt)
    	addChild(txt)
    	
    	removeChild(txt2)
    	addChild(txt2)
    	txt2.text = "click to unload picture"
    }
    function clickHandler(event:MouseEvent):void {
    	var loader:Loader = Loader(event.target);
    	loader.content.bitmapData.dispose()
    	removeChild(loader)
    	loader.unload();
    	loader=null
    	txt2.text = "click to load picture"
    }
    
    addEventListener(Event.ENTER_FRAME,enterframe)
    function enterframe(e:Event){
    	txt.text = "total memory: " + System.totalMemory/10000
    }

  2. #2
    Junior Member
    Join Date
    Jul 2009
    Posts
    5
    As an as3 expert here are my ideas howyou could improve your code:
    1) Use weakly referenced listeners.
    use
    Code:
    loader.addEventListener(MouseEvent.CLICK, clickHandler, false, 0, true );
    instead of
    Code:
    loader.addEventListener(MouseEvent.CLICK, clickHandler);
    and for any other addEventListener.

    2) Try to add this code:
    // force immediate garbage collection - see grant skinner's article at
    // http://www.gskinner.com/blog/archive...urce_ma_2.html
    Code:
    try {
    				   new LocalConnection().connect('foo');
    				   new LocalConnection().connect('foo');
    } catch (e:*) {}
    and see if the memory drops. You should better watch the memory, one load after another.

    3) Remove event listeners on what you added: loader.addEventListener(MouseEvent.CLICK, clickHandler);

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