Flash/AS3 URLStream Memory Leak

Lately we have been doing some work with persistent connections. If you are familiar with Comet the Flash/AS3 URLStream class provides an interesting alternative. The URLStream class exposes raw binary data as it is downloaded.

Unfortunately, this week we ran into a rather tricky memory leak when using this nifty class. An event listener was subscribed to the progress event and over time memory usage steadily increased to a point of making the browser inoperable.

After a little digging we narrowed the problem down to the URLStreams usage of the ByteArray. It seems as if URLStream was reallocating a buffer for the array and the short turn around time (on the reads) was not giving the garbage collector enough time to throw out the old allocation.

The way the leak could be corrected was by deleting the ByteArray (Set null), forcing garbage collection of the read buffer.

Here is the workaround:

var bytes:ByteArray = new ByteArray();
this.readBytes(bytes, 0, this.bytesAvailable);
bytes = null;

Happy Streams!

Carl Yestrau

Posted by