2 Replies Latest reply: Oct 11, 2010 7:12 PM by odyssey27 RSS

    fileReference.data.clear not working?

    jiinus Community Member

      Hi,

       

      First of all, sorry if this is the wrong community for this question. If so, feel free to move it to the proper community.

       

      I'm making a sort of a desktop publishing application with Flash 10. The user needs to be able to select potentially hundreds of image files, which are then viewed, modified and laid out using the bytedata retrieved with FileReference.load. There might be (virtually) an infinite number of files used at a given session, so I would like to minimize memory usage by only storing the references to the files, load only the ones that are needed at a time and dispose the data of the FileReference objects that are not needed. However, the clear() method of the ByteArray loaded by the FileReference object doesn't seem free up any memory. Is this a bug in SDK/Flash Player or am I just not getting something here?

       

      Here is a sample code. Run it with FB profiler with flash.* filter removed and click on the red square to progress file loading steps. On the file browse dialog, select a fair amount of preferably large image files to get ample memory consumption. After the code finishes, I would expect the memory consumption of the ByteArray class to drop and the overall memory to be almost at the level it was before the loading operation, but it doesn't seem to work (at least in Mac / FF / Safari / AIR or Windows 7 / FF / AIR). Flash Player used is 10,1,53,64.

       

      package
      {
          import flash.display.Sprite;
          import flash.display.StageAlign;
          import flash.display.StageScaleMode;
          import flash.events.Event;
          import flash.events.MouseEvent;
          import flash.net.FileReference;
          import flash.net.FileReferenceList;
          import flash.text.TextField;
          import flash.text.TextFieldAutoSize;
         
          [SWF(width="100", height="100", backgroundColor="0xffffff", frameRate="30")]
          public class FileReferenceTest extends Sprite
          {
             
              private var clickArea:Sprite;
             
              private var fileRef:FileReferenceList;
              private var files:Vector.<FileReference>;
             
              private var tf:TextField;
              private var memTF:TextField;
             
              private var numFilesToLoad:int = 0;
              private var numFilesLoaded:int = 0;
             
              public function TestBed()
              {
                 
                  stage.align = StageAlign.TOP_LEFT;
                  stage.scaleMode = StageScaleMode.NO_SCALE;
                 
                  fileRef = new FileReferenceList();
                  fileRef.addEventListener(Event.SELECT, onFileSelect);
                 
                  clickArea = new Sprite();
                  clickArea.graphics.beginFill(0xff0000);
                  clickArea.graphics.drawRect(0, 0, 100, 100);
                  clickArea.graphics.endFill();
                  clickArea.addEventListener(MouseEvent.CLICK, onBrowseClick);
                  addChild(clickArea);
                 
                  tf = new TextField();
                  tf.text = 'Browse files';
                  tf.autoSize = TextFieldAutoSize.CENTER;
                  tf.x = (clickArea.width - tf.textWidth) / 2;
                  tf.y = (clickArea.height - tf.textHeight) / 2;
                  tf.mouseEnabled = false;
                  addChild(tf);
                 
              }
             
              private function onBrowseClick(event:MouseEvent):void
              {
                  fileRef.browse();
              }
             
              private function onFileSelect(event:Event):void
              {
                  files = new Vector.<FileReference>();
                 
                  tf.text = 'Load files';
                 
                  clickArea.removeEventListener(MouseEvent.CLICK, onBrowseClick);
                  clickArea.addEventListener(MouseEvent.CLICK, onLoadFilesClick);
                 
              }
             
              private function onLoadFilesClick(event:Event):void
              {
                 
                  tf.text = 'Loading...';
                 
                  clickArea.alpha = 0.2;
                  clickArea.buttonMode = false;
                  clickArea.mouseEnabled = false;
                 
                  numFilesToLoad = 0;
                  numFilesLoaded = 0;
                 
                  for (var i:int = 0; i < fileRef.fileList.length; i++) {
                      files.push( fileRef.fileList[i] );
                      files[i].addEventListener(Event.COMPLETE, onFileLoaded);
                      files[i].load();
                      numFilesToLoad++;
                  }
                 
              }
             
              private function onFileLoaded(event:Event):void
              {
                  numFilesLoaded++;
                 
                  if (numFilesLoaded == numFilesToLoad) {
                      clickArea.alpha = 1;
                      clickArea.buttonMode = true;
                      clickArea.mouseEnabled = true;
                     
                      tf.text = 'Clear bytes';
                     
                      clickArea.removeEventListener(MouseEvent.CLICK, onLoadFilesClick);
                      clickArea.addEventListener(MouseEvent.CLICK, onClearBytesClick);
                  }
              }
             
              private function onClearBytesClick(event:Event):void
              {
                 
                  var numBytesCleared:int = 0;
                 
                  for (var i:int = 0; i < files.length; i++) {
                      numBytesCleared += files[i].data.length;
                      files[i].data.clear();
                  }
                 
                  tf.text = numBytesCleared + "\nbytes cleared";
                 
              }
             
          }
      }

       

      Any help would be welcome!