0 Replies Latest reply on Dec 17, 2009 6:01 AM by ExeyPanteleev

    How to clean the memory after calling BitmapData.draw() ?

    ExeyPanteleev

      If you add the embedded image on the Stage 800x600 flash player will spend 17Mb RAM / 14Mb Virtual

      But if you create 1200 sprites (in every 100 circles), call draw (), remove the sprites and force to call Garbage Collector, flash player will spend much more - 36Mb RAM / 33Mb Virtual

       

      var container:Sprite = new Sprite();
       
      // adding 1200 sprites
      var shiftX:Number = 10;
      
      var shiftY:Number = 10;
      for (var i:int = 0; i < 1200; i++) {
      
           var circle:Sprite = new Sprite();
           // drawing 100 circles
           for (var j:int = 0; j < 100; j++) {
      
                circle.graphics.beginFill(0xFFFFFF/j*i);
                circle.graphics.drawCircle(0, 0, 10-j*.1);                         
           }
      
           circle.x = shiftX;
           circle.y = shiftY;
           shiftX += 20;
           if ( shiftX >= stage.stageWidth ) {
      
                shiftX = 10;
                shiftY += 20;
           }
           container.addChild(circle);
      }
      
       
      // draw() and adding bitmap
      var bmd:BitmapData = new BitmapData(800, 600, true, 0x00FFFFFF);
      bmd.draw(container);
      
      var bmp:Bitmap = new Bitmap(bmd, "auto", true);
      addChild(bmp);
      
       
      // removing sprites
      while (container.numChildren) container.removeChild(container.getChildAt(0));
      
       
      // Force Garbage Collector 1
      System.gc();
      System.gc();
       
      // Force Garbage Collector 2
      try {
      
           new LocalConnection().connect('foo');
           new LocalConnection().connect('foo');
      
      } catch (e:Error) {}

      What happens to memory?