title: AS3 drawing Sprite on BitmapData memory leak
body: <p>I have an AIR app that renders images from a Flash editor. You can customize several surfaces - they all have the same width and height. Each surface is then rendered by the AIR app.</p>
  
  <p>I'm struggling with a memory leak I fail to address.</p>
  
  <p>For each surface I need to render, I have a Sprite containing many components (other sprites - some having event listeners, BitmapDatas and other children components, sprites, etc).</p>
  
  <p>I'm already aware of BitmapData issues to be garbage collected, I tested both :</p>
  
  <ul>
  <li>creating a new BitmapData for each rendering then dispose() and point to null</li>
  <li>reuse a single BitmapData for each rendering</li>
  </ul>
  
  <p>Memory leak is still happening in equal proportion.</p>
  
  <p>Here is my loop :</p>
  
  <pre><code>var bm:BitmapData = new BitmapData(destDim.x, destDim.y, true, bgColor);
  var mtx:Matrix = new Matrix();
  trace('before drawing :'+(System.privateMemory/1024));
  bm.draw(myBigSprite, mtx, null, null, null, true);
  trace('after drawing :'+(System.privateMemory/1024));
  var result:Bitmap = new Bitmap(bm, PixelSnapping.NEVER, true);
  
  //return result and encode Bitmap to png
  
  result.bitmapData.dispose();
  result.bitmapData = null;
  result = null;
  </code></pre>
  
  <p>Result :</p>
  
  <pre><code>before drawing :208364
  after drawing :302816
  Wrote bitmap to file: surface0.png
  before drawing :303296 
  after drawing :446160 
  Wrote bitmap to file: surface1.png 
  before drawing :446160
  after drawing :565212
  Wrote bitmap to file: surface2.png 
  before drawing :565924
  after drawing :703100 
  Wrote bitmap to file: surface3.png 
  before drawing :703572
  after drawing :834420 
  Wrote bitmap to file: surface4.png 
  </code></pre>
  
  <p>I feel like I'm missing something in draw function behaviour. It seems like I have newly created instances of the components of myBigSprite that persists after draw operation.</p>
  
  <p>I tried to completely destroy myBigSprite at the end of each loop, it does not change anything....</p>
  
  <p>Any hint would be appreciated !</p>