FLVPlayback Memory Leak – Fixed
So the Flash CS3 FLVPlayback control has a memory leak. Based on prior experiences with Flash components, I'm not too shocked.... here's my solution.
I'm working on a straight AS3 project that has a dynamically generated pop-up type window which can contain a video player. You open the window, a video player is created - you close it, and the player is removed. The client requested that we use Flash's FLVPlayback component. When I started doing some profiling I noticed that instances of FLVPlayback and its subcomponents weren't getting garbage collected. After several hours of frustration, I created a project from scratch that just loaded and unloaded FLVPlaybacks to see if I could recreate the problem. Here it is:
You'll notice that each time you create a video player, System.totalMemory goes up by several megs! (your results may vary) and it never comes back down. Here's what it looks like after creating and destroying 10 FLVPlaybacks:
Note that I sorted by current number of instances, and removed flash.*.* from the filters list so that I could see the Timers.
All of the objects in the fl.video.skin package are still hanging around. I actually have no idea why it created two skins for each instance, but I'm sure there is a good explanation... the main point is that none of these objects were getting garbage collected.
It took a lot of rooting around in the source before figuring out what the culprits were, but I knew I would have to do some manual cleanup work in order to clear out all object references. My solution was to subclass FLVPlayback and implement a dispose() method to do the necessary cleanup. (Just for fun, I got the same solution working with an old school prototype hack, but I think we're supposed to pretend those don't exist anymore.) Now, I may have been a little aggressive on my cleanup - but the overhead is tiny, and it's important to me that this always works. If anybody knows of a simpler solution, please post it in the comments. Here's the breakdown:
- Call stop(). Obviously if the video is still playing it cannot be garbage collected.
- Stop all timers in FLVPlayback and UIManager and remove the TimerEvent.TIMER event listeners attached to each one.
- Call the close() method on the VideoPlayer instances. This will stop their timers, remove event listeners, and close NetConnections.
Here's the same application, but using FLVPlayback2 and the FLVPlayback2.dispose() method:
And here are the profiler results:
Success! I wish that hadn't taken so much detective work... why aren't the Flash components built to the same standards as the Flex ones?
About this entry
You’re currently reading “FLVPlayback Memory Leak – Fixed,” an entry by Ryan Phelan
- Published:
- 09.22.08 / 2pm
- Category:
- Flash








8 Comments
Jump to comment form | comments rss [?] | trackback uri [?]