8 Replies Latest reply on Aug 12, 2009 1:03 PM by 7546445

    RAM Problem of Bad SWC's

    7546445 Level 1

      Abstract

      So there are lots of grated swc's tuday... But some of them are done with troubles...  (Lots of troubles...)

      But they work! They  do what was expected from them.

      So If SWC function works but after its work some trash byte stays in your RAM... What can you do?

       

      Problem

      How to clean up ram used by function after that function has  finished its work?

       

      Example

      I'm using some  alchemy synchronous jpeg  encoder (like here ) to convert some MXML component to Jpeg... It works Fast! it's grate I'm love in it BUT after I use that function 1000 times on 1024x1024 or any other size it fulls up all my RAM!!!(((

       

      Code

       

       

      private function CJpegWay(e:Event):ByteArray {

       

      var bmpdata:BitmapData = new BitmapData(mySource.width,  mySource.height);

              bmpdata.draw(mySourceConteiner);

      var timer:int = getTimer();

      var ba:ByteArray = bmpdata.getPixels(new Rectangle(0,0, mySource.width,  mySource.height));

      var jpegBA:ByteArray= as3_jpeg_wrapper.write_jpeg_file(ba, mySource.width,  mySource.height, Some.value,SomeOther.value); /* If We'll comment "= as3_jpeg_wrapper.write_jpeg_file(ba, mySource.width,  mySource.height, Some.value, SomeOther.value)" no ram overflow will happen */

       

      it.text = ("   !!! C  JPEG ENCODING: !!! "+(getTimer()-timer)+"ms.").toString() + " "  + " jpegBA.length=" + jpegBA.length.toString();

      return jpegBA;

      }

       

       

      Error

       

      Error: Error #1000: The system is out of memory.

      at flash.display::BitmapData/getPixels()

      at HtmlTester/CJpegWay()[C:\Users\Avesta-V\Adobe Flash Builder Beta\COAirBrV2\src\HtmlTester.mxml:92]

      at HtmlTester/timerHandlerJpeg()[C:\Users\Avesta-V\Adobe Flash Builder Beta\COAirBrV2\src\HtmlTester.mxml:147]

      at flash.utils::Timer/_timerDispatch() at flash.utils::Timer/tick() 

       

      or Your RAM will be just Full or trash...

       

      So Can Any One HELP ME and HELP ALL OF US... PLEASE...

        • 1. Re: RAM Problem of Bad SWC's
          David_F57 Level 5

          i'm not sure why you would want to save 1000 images in RAM before compression which is what you would be doing twice,

           

           

          var bmpdata:BitmapData = new BitmapData(mySource.width,  mySource.height);

                  bmpdata.draw(mySourceConteiner);


           

           

          var ba:ByteArray = bmpdata.getPixels(new Rectangle(0,0, mySource.width,  mySource.height));

           

          plus about 1000*1mb for the jpegs = 1gb


           

          sometimes its a good idea to work out just what you are doing with memory before you blame components etc..

           

          the code below will do what you want to do without ..the duplication

           

           


           

           

             private var jpg:JPEGEncoder = new JPEGEncoder(80);

             private var objBitmapData:BitmapData;

           

             protected function button1_clickHandler(event:MouseEvent):void

             {

               objBitmapData=ImageSnapshot.captureBitmapData(drawsource);

               var pngData:ByteArray=jpg.encode(objBitmapData);

             }

           

          David

          • 2. Re: NOT HELPFUL.(
            7546445 Level 1

            still ram is filling up with that function... Actually ImageSnapshot is not kind of sum Super Fast thing... actually 60 - 100 ms = 15 - 10  times per second that's all the time I can give to that function... so... and it's not actually the way to clean up ram after function has fired... and that lines do not eat ram...  Try to play with http://segfaultlabs.com/blog/post/asynchronous-jpeg-encoding I hope you'll find that interesting...  Open link go way to the futter and you'll see  ** update **  with text like "This is updated version of libjpeg port. You can now compare JPEG encoding times for alchemy asynchronous, alchemy synchronous, bytearray.org, Kyle Lu and buildin mx.* encoders. The difference in the size of a file can also be noted. Right click for code view."

            so generate a bit map try different functions of encoding... at the bottom youl see create JPEG alchemy sync ... open tool for ram monitoring and click on  create JPEG alchemy sync 40 - 60 times  70 - 100 mbs...  or more...)  CLICK VIEW SOURCE

            go to the function  alchemySync()...

            http://segfaultlabs.com/swf/alchemy/alchemyJpegUpdate_sv/index.html  

             

                private function alchemySync():void
                         {
                            ba = bitmapData.getPixels( bitmapData.rect );
                            ba.position = 0;
                                if ( baout )        
                                    baout.length = 0;
                                else 
                                    baout = new ByteArray();
                            
            
                             timer = flash.utils.getTimer();
            
                            lib.encode( ba, baout, bitmapData.width, bitmapData.height, 80 );
                            
                            timer = flash.utils.getTimer() - timer;
                            
                            memo1.text += "\nJPEG compression time (alchemy sync) : " + timer + "ms";
                            memo1.text += "\nJPEG size : " + baout.length;
                            fname = "jpeg_sync_alchemy.jpg"
                            
                            saveBtn.label = "save alchemy sync jpeg";
                            saveBtn.enabled = true;
                         };
            • 3. Re: NOT HELPFUL.(
              David_F57 Level 5

              You stopped redeclaring and duplicating the bitmapdata now, so something must have been helpful

               

              Try using debugging, you are very quick at placing blame before looking at your code, just like your previous "critical" error with mx:list posting, if you had bothered to debug your code in any of the sdk's you had claimed to use, all of them would have shown the ERROR in your code immediately.

               

              David.

              • 4. Re: NOT HELPFUL.(
                7546445 Level 1

                Oh...I know flex baddd... whatsoever I want to learn!) I'm trying I was searching for the way to clean Ram using AS not for error searching using debugger... but thanks i know that after 50th time I use function I'd beter look at all the variables and see what one is containing  100 mbps of data... Would you mind telling me how to show debugger only on 50th call to the function (if it's possible) =)

                • 5. Re: RAM Problem of Bad SWC's - i have a solution you can try it
                  ddelgorgue2.0

                  Same memory leak here. For my project, is critical because users can upload in album 12m pixels pictures.

                  pictures encoded and going in serveur. And in flash appli users can only view small version from uploaded pictures.

                  (with php library gd)

                   

                  I am sure the issue is how swc is implented in flash.

                  for isolate that, i put a comment in encode line : i check memory. No leak.

                  i revalidate encode line : memory leak. (800megas afters 10 upload)

                   

                  but with fs10 we have unloadandStop for swf.

                  so....you can use class for encode and swc in a separate swf, and after job use unloadandStop for clear swf.

                   

                  in firefox, no memory leak, in ie memory not climb after the first upload.(after 10 upload i stay in 180megas range)

                   

                  main swf :

                   

                  picture upload with a filereference.load() and after a loader read in a bytArray picture data.

                   

                  _loaders[evt.target].contentLoaderInfo.removeEventListener(Event.COMPLETE,_onImageLoaded);
                  var bmpbmp:Bitmap = Bitmap(evt.target.content);
                  myBitmapData = bmpbmp.bitmapData;
                   ba = myBitmapData.getPixels(myBitmapData.rect);
                  ba.position = 0;
                  baout = new ByteArray  ;
                  _loaders[evt.target].unload();
                  delete _loaders[evt.target];
                  

                   

                  i use a loader for load a swf. in the swf i put swc for encode picture. (so in my main swf i not add library and not compile swc)

                  
                  var loaderSWF = new Loader  ;
                  _loaders[loaderSWF.contentLoaderInfo] = loaderSWF;
                  loaderSWF.contentLoaderInfo.addEventListener(Event.COMPLETE,loadswfcomplete,false,0,true);
                  var request:URLRequest;
                  var url:String = "compressor.swf";
                  request = new URLRequest(url);
                  loaderSWF.load(request);
                  
                  private function loadswfcomplete(pEvt:Event):void {
                              var clip:MovieClip = pEvt.target.content as MovieClip;
                              _loaders[pEvt.target].contentLoaderInfo.removeEventListener(Event.COMPLETE,
                  loadswfcomplete);
                              baout=clip.encode(ba,myBitmapData.width,myBitmapData.height,100);
                              clip=null;
                              _loaders[pEvt.target].unloadAndStop();
                              delete _loaders[pEvt.target];
                              compressFinished();
                  }
                  
                  private function compressFinished() {
                              myBitmapData.dispose();
                              ... some code for upload with urlRequest jpg on serveur
                          }
                  
                  private function fileUploaded(evt:Event):void {
                  ... some code after picture encoded is on serveur
                              jpgURLRequest.data.length = 0;
                              jpgURLRequest.data=new ByteArray();
                              jpgURLRequest = null;
                              if (ba) {
                                  ba.length = 0;
                              }
                              if (baout) {
                                  baout.length = 0;
                              }
                              ba=new ByteArray();
                              baout=new ByteArray();
                              flash.system.System.gc();
                              flash.system.System.gc();
                          }
                  

                   

                  and the class in compressor.swf :

                  package {
                      import flash.display.MovieClip;
                      import cmodule.jpegencoder.CLibInit;
                      public class compressor extends MovieClip {
                          private var lib:Object;//alchemy library 
                         
                          public function compressor() {
                              this.addEventListener(Event.ADDED_TO_STAGE,init,false,0,true);
                          }
                          //// initialisation
                          private function init(pEvt:Event) {
                              this.removeEventListener(Event.ADDED_TO_STAGE,init,false,0,true);
                              // for futur code....
                          }
                  
                          public function encode(ba:ByteArray,xx:int,yy:int,q:int):ByteArray {
                              var _init:CLibInit = new CLibInit  ;//get library obejct
                              lib = _init.init();// initialize library exported class 
                              var baout:ByteArray=new ByteArray();
                              ba.position = 0;
                              lib.encode(ba,baout,xx,yy,q);
                              return baout;
                          }
                  
                      }
                  }
                  

                  only compressor.swf contains link at swc and was compiled with swc. nothing in main swf.

                  1 person found this helpful
                  • 6. Re: RAM Problem of Bad SWC's - i have a solution you can try it
                    7546445 Level 1

                    It is helpful it works better but similar to  GC's (in air) - too slow I need to perform this operation 30 times a second on 2000x 2000 images (and GC's i use only on when user mem is near to critical point...))

                    This method has its benefit - asink streaming!) and what do you think about modules?

                    • 7. Re: RAM Problem of Bad SWC's
                      ddelgorgue2.0 Level 1

                      dont know why module cause memory leak because in C the code for clear memory exist.

                       

                       

                      but for apply you use :

                       

                      you can in main apply :

                      load swf for compression.

                      make compression

                      check memory in flash

                      make compression

                      check memory in flash

                      etc...

                      ......

                      and when memory is superior a limit you fixe :

                      > unloadandstop the compressor swf

                      > add on stage a timer event

                      > wait in the timer  memory clean

                       

                      and reload swf compressor

                      > make again a cycle compression

                       

                      with cache load compressor again is fast.