3 Replies Latest reply on Nov 1, 2006 9:00 AM by Speckracer

    Memory leak using XMLSocket

    @lissa
      I have a Flash application (client) that needs to connect to a socket server via a wireless connection.

      This connection can drop out if, eg, the client PC moves out of range, and so the Flash application needs to be able to automatically reconnect.

      The code block attached is a stripped down version of the Flash 8 to accomplish this.

      This code block is now the ONLY thing that I have in the Flash movie (on 1st frame of _root timeline, there's nothing on the stage, no other code, nothing in the library etc) and it is leaking memory at quite a significant rate:

      I have also tried a very similar version of this code, instead using 3rd party Northcode's SWFStudio (swf2exe wrapper) TCP sockets and a similar memory leak occurs.

      I have also tried adding a "stop" command to halt (and delete) the interval and although the memory usage stops increasing, it does not return to its pre-connect level.

      This Flash application needs to run indefinately so obviously memory leakage is bad, particularly seeing as this Flash application may, in some instances, run on a PDA.

      Does anyone have any suggestions?

      I believe there were meant to be significant improvements to the garbage collection in Flash 8 however it obviously didn't resolve this issue. Will it be resolved in Flash 9?


        • 1. Re: Memory leak using XMLSocket
          blemmo Level 1
          What happens if you don't delete and rebuild the socket, but just close it and then connect again? Does it reconnect that way? I remember having some troubles with XMLSocket, but don't know anymore if it was similar...
          Maybe it isn't such a good idea to recreate the socket in such a short interval. What about using the onClose event to determine when the connection is closed, and only then reconnect the socket? This should be way more ressource friendly, although I'm not sure if the onClose will trigger when the connection isn't closed by the remote socket, but broken otherwise. Maybe you can give it a try.
          BTW, the Flash Player 9 is already released, so you can try and see if it behaves the same. Just go to the download page to get it. I found this tool quite helpful for dealing with different Flash Player versions: FlashPluginSwitcher.

          greets,
          blemmo
          • 2. Re: Memory leak using XMLSocket
            Wolf_van_Ween Level 1
            We had a discussion about XML connections eating up resources a while ago, there seemed to be no remedy other than drastically reducing the number of times a new Socket is being produced. In particular deleting the socket was definitely not freeing the memory.
            What I do understand is that you need very regular polling. What I don't understand is why you need to close and open the connection on each one of those. Wouldn't it be better to employ some sort of protocol, in which you send a message to the other side and wait with a certain timeout for a response. Only if this response doesn't come in time do you re-open the connection.
            But then I don't know of course how accessible "the other side" is to you.
            Good luck
            Wolf
            • 3. Re: Memory leak using XMLSocket
              Speckracer
              the problem is that you use the setInterval() function. That is known to cuase a leak if you don't close it properly or keep calling it. The documentation for setInterval will tell you this as well.