4 Replies Latest reply on Mar 26, 2008 9:34 AM by kingersoll

    Many instances of URLRequest/URLLoader

    kingersoll
      I'm working on an AIR application using HTML/Javascript which will potentially create hundreds, maybe thousands, of new instances of URLRequest and URLLoader during the application's execution. Will there be any potential memory leak problems with this? Should I reuse one instance of each? Is there any way to free the URLRequest/URLLoader memory once the request is complete, or is it done automatically?
        • 1. Re: Many instances of URLRequest/URLLoader
          anirudhs Level 2
          Hi,

          There won't be memory leaks if you make sure there are no more references pointing to that particular URLLoader, all event listeners to it are weak or removed after use, etc. The garbage collector will pick it up and free up memory.

          But I would suggest re-using one (or two) instances of URLLoader since there's probably a bound on the upper number of simultaneous url requests. That is, you could make the calls simultaneously, but internally they would queue up after two requests have gone in. This is the behavior with most browsers (the RFC for HTTP suggests a maximum of two simultaneous socket connections).

          But I have not confirmed this with the flash player or AIR. Even then, it would be best to have two or three URLLoader's that you re-use.
          • 2. Re: Many instances of URLRequest/URLLoader
            kingersoll Level 1
            Thank you for the reply. I don't plan on doing multiple simultaneous connections, it will be one load at a time - but looping this hundreds or thousands of times, creating a new URLLoader and URLRequest each time raised some questions about the garbage collector.

            Is there any way to quickly remove all event listeners without having to specify the function name(s)? I often use unnamed functions (I forget the correct terminology) for my event listeners as they may change dynamically.
            • 3. Re: Many instances of URLRequest/URLLoader
              anirudhs Level 2
              Function closures. There's no way to remove all event listeners for a particular event, but you can use something like this in your anonymous function:

              btn.addEventListener(MouseEvent.CLICK, function (event:Event):void
              {
              Alert.show("listener");
              event.target.removeEventListener(MouseEvent.CLICK, arguments.callee);

              });

              arguments.callee refers to the current function.

              Note: If you set weak references as true for function closures, it might lead to unpredictable results:

              quote:

              In the case of function closures, setting useWeakReference to true as the last argument can lead to unexpected results. To Flex, an inner function is actually an object, and can be freed by the garbage collector. If you set the value of the useWeakReference argument to true, as shown in the previous example, there are no persistent references at all to the inner function. The next time the garbage collector runs, it might free the function, and the function will not be called when the event is triggered.

              If there are other references to the inner function (for example, if you saved it in another variable), the garbage collector will not free it.
              .

              • 4. Re: Many instances of URLRequest/URLLoader
                kingersoll Level 1
                quote:

                arguments.callee


                This is exactly what I needed. Thanks! :)