9 Replies Latest reply on Jan 9, 2010 5:46 AM by FarafraSo

    NetConnection and memory leak

    Farafra

      Hi,

       

      I've got a memory leak problem with NetConnection when using with amf.

       

      Here is the code :

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  layout="absolute">
           <mx:Script>
                <![CDATA[
                     private var rc:NetConnection;
                     
                     
                     private function send():void     
                     {
                          var responder:Responder = new Responder( onResult, onFault );
      
                          rc = new NetConnection();
                          rc.objectEncoding = ObjectEncoding.AMF3;
                          rc.connect("http://localhost/gateway.php");
                          rc.call("Test.testParam", responder , 'val');
                          
                     }
                     
                     private function onResult(obj:Object):void     {
                          
                          rc.close(); 
                     }
                     
                     private function onFault():void
                     {
                          rc.close();
                     }
                     
                ]]>
           </mx:Script>
           <mx:Button label="Send" click="send();" />
      </mx:Application>
      

       

      So the more i call send(), more the memory grows without being garbage collected.

      Does anyone see where could be the fault ?

       

      I am under linux whith Flash 10.0 r32 in debug mode and flex 3.4.

      I have checked the amount of memory with this class :

      http://code.google.com/p/mrdoob/wiki/stats

       

      Thanks,

       

      Thierry

        • 1. Re: NetConnection and memory leak
          Flex harUI Adobe Employee

          What is leaking?

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: NetConnection and memory leak
            Farafra Level 1

            >What is leaking?

             

            As i was speaking of memory leak i am not sure to really understand your question or the information you need.

            • 3. Re: NetConnection and memory leak
              oldMster Level 3

              The problem may be that you have multiple 'pending' requests, with reference still made 'behind the scenes' to the incomplete send calls. 

              The code below disables the 'send' button until a call has either completed or faulted, see if this resolves the problem.

              Mark

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  layout="absolute">
                   <mx:Script>
                        <![CDATA[
                             private var rc:NetConnection;
                             
                             
                             private function send():void     
                             {
                                  var responder:Responder = new Responder( onResult, onFault );
              
                                  rc = new NetConnection();
                                  rc.objectEncoding = ObjectEncoding.AMF3;
                                  rc.connect("http://localhost/gateway.php");
                                  rc.call("Test.testParam", responder , 'val');
                                  btnSend.enabled=false;
                             }
                             
                             private function onResult(obj:Object):void     {
                                  
                                  rc.close(); 
                                  btnSend.enabled=true;
                             }
                             
                             private function onFault():void
                             {
                                  rc.close();
                                  btnSend.enabled=true;
                             }
                             
                        ]]>
                   </mx:Script>
                   <mx:Button label="Send" click="send();" id="btnSend" />
              </mx:Application>

              • 4. Re: NetConnection and memory leak
                Farafra Level 1

                > The problem may be that you have multiple 'pending' requests

                 

                Yes, you are right to mention it but the problem doesn't come from here.

                Maybe i should have gave a more clear and complete example.

                Here is one with automation and memory usage :

                 

                For information the memory grows a little less than 2MB each 100 calls and is never garbage collected.

                 

                 

                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >
                     <mx:Script>
                          <![CDATA[
                               import mx.managers.CursorManager;
                               
                               private var rc:NetConnection;
                               private var testRun:Boolean;
                               
                               [Bindable]
                               private var nbCall:int;
                               [Bindable]
                               private var memoryUsage:String;
                               
                               
                               private function start():void
                               {
                                         if ( true === testRun ) {
                                              return;
                                         }
                                         
                                         nbCall = 0;
                                         CursorManager.setBusyCursor();
                                         testRun = true;
                
                                         send();
                               }
                               
                               private function stop():void
                               {
                                         testRun = false;
                               }
                               
                               private function send():void     
                               {
                                    var responder:Responder = new Responder( onResult, onFault );
                                    rc = new NetConnection();
                                    rc.objectEncoding = ObjectEncoding.AMF3;
                                    rc.connect( "http://localhost/gateway.php" );
                                    rc.call( "Test.testParam", responder , "val");
                
                                    nbCall++;
                               }
                               
                               private function onResult(obj:Object):void     {
                                    
                                    rc.close(); 
                                    memoryUsage = String( ( System.totalMemory * 0.000000954 ).toFixed( 3 ) ) + " MB";
                                    
                                    if ( true === testRun ) {
                                         send();
                                    }
                                    else {
                                         cursorManager.removeBusyCursor();
                                    }
                               }
                               
                               private function onFault():void
                               {
                                    rc.close();
                                    cursorManager.removeBusyCursor();
                               }
                               
                          ]]>
                     </mx:Script>
                     <mx:VBox>
                          <mx:Label text="{'Number of call: ' + String( nbCall )}" />
                          <mx:Label text="{'Memory usage: ' + memoryUsage}" />
                          <mx:Button label="Start Test" click="start();" />
                          <mx:Button label="Stop" click="stop();" />
                     </mx:VBox>
                </mx:Canvas>
                
                • 5. Re: NetConnection and memory leak
                  Flex harUI Adobe Employee

                  It is very easy to get sparsely populated memory pages in Flash.  Simple transactions like this will not always settle back to the original starting point.  Use the profiler to see if there really is a leak, or run 10000's of tests through this code to see if memory usage grows unbounded.  Usually it will settle down eventually.

                   

                  Alex Harui

                  Flex SDK Developer

                  Adobe Systems Inc.

                  Blog: http://blogs.adobe.com/aharui

                  • 6. Re: NetConnection and memory leak
                    Farafra Level 1

                    > Use the profiler to see if there really is a leak, or run 10000's of tests through this code to see if memory usage grows unbounded

                     

                    Unfortunatly there is no profiler under linux...

                    With 10 000 calls the memory grows up from 15 to 210 MB and is never garbage collected, even after 15 minutes; by the way if adobe need beta tester do not hesitate to ask as i am a freelance ^^

                     

                    More seriously and because you don't tell me i've done a bad implementation of the NetConection object, is the bug belong to the linux player or the flex sdk ?

                    Do you know if this bug also reside on the windows and/or mac plateform ?

                     

                    Thanks,

                     

                    Thierry

                    • 7. Re: NetConnection and memory leak
                      Flex harUI Adobe Employee

                      Hard to say where the bug is.  I noticed you're making a new NetConnection object on each send instead of reusing one.  But without a small complete test case it is hard to say just by visual inspection.  The code you posted starts with Application and ends with Canvas.

                       

                      Alex Harui

                      Flex SDK Developer

                      Adobe Systems Inc.

                      Blog: http://blogs.adobe.com/aharui

                      • 8. Re: NetConnection and memory leak
                        Farafra Level 1

                        >I noticed you're making a new NetConnection object on each send instead of reusing one

                        You're right to notice, but the result is the same.

                         

                        The next follow here :

                        http://bugs.adobe.com/jira/browse/SDK-24820

                        • 9. Re: NetConnection and memory leak
                          FarafraSo

                          The bug has been moved to the flash player and it is confirmed that the NetConnection Object has a memory leak.

                           

                          Please vote to make it fixed :

                          http://bugs.adobe.com/jira/browse/FP-3596

                           

                          Thierry