Flash/AS3 URLStream Memory Leak
| Topics: | dev, ui |
|---|---|
| Tags: | |
| Share: |
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!

December 5th, 2007 at 5:24 pm
[...] Streams! - Posted by [...]
February 23rd, 2008 at 5:58 am
Hi,
I found this memory leak on my application and tried to apply your workaround but it’s not working to me. Could you please give further details on it? Should I subclass URLStream and use that code in which method of the subclass? (I noticed your use of “this”, that’s why I’m asking this).
Thanks a lot for your time…
February 23rd, 2008 at 7:08 am
Forget about it… I’ve already solved it, I had another unrelated problem.
Thanks for the information!