<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ryan Phelan &#187; FLVPlayback</title>
	<atom:link href="http://www.rphelan.com/tag/flvplayback/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rphelan.com</link>
	<description>Exploring Flex, Design, and Visualization</description>
	<lastBuildDate>Thu, 12 Aug 2010 14:56:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>FLVPlayback Memory Leak &#8211; Fixed</title>
		<link>http://www.rphelan.com/2008/09/22/flvplayback-memory-leak-fixed/</link>
		<comments>http://www.rphelan.com/2008/09/22/flvplayback-memory-leak-fixed/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 19:46:42 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Custom Components]]></category>
		<category><![CDATA[FLVPlayback]]></category>
		<category><![CDATA[Memory Management]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Profiling]]></category>

		<guid isPermaLink="false">http://www.rphelan.com/?p=107</guid>
		<description><![CDATA[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 - [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p>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:</p>
<p><a href="http://www.rphelan.com/flex/FLVMemoryLeak/FLVMemoryLeak.html">Application</a> -  <a href="http://www.rphelan.com/flex/FLVMemoryLeak/srcview/index.html">Source</a></p>
<p>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:</p>
<p><a href="http://www.rphelan.com/flex/uploads/2008/09/flv-leak.jpg"><img class="alignnone size-full wp-image-109" title="flv-leak" src="http://www.rphelan.com/flex/uploads/2008/09/flv-leak.jpg" alt="" width="500" height="477" /></a></p>
<p><em>Note that I sorted by current number of instances, and removed flash.*.* from the filters list so that I could see the Timers.</em></p>
<p>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.</p>
<p>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:</p>
<ol>
<li>Call stop().  Obviously if the video is still playing it cannot be garbage collected.</li>
<li>Stop all timers in FLVPlayback and UIManager and remove the TimerEvent.TIMER event listeners attached to each one.</li>
<li>Call the close() method on the VideoPlayer instances.  This will stop their timers, remove event listeners, and close NetConnections.</li>
</ol>
<p>Here's the same application, but using FLVPlayback2 and the FLVPlayback2.dispose() method:</p>
<p><a href="http://www.rphelan.com/flex/FLVMemoryLeak/FLVMemoryLeakFixed.html">Application</a> -  <a href="http://www.rphelan.com/flex/FLVMemoryLeak/srcview/index.html">Source</a></p>
<p>And here are the profiler results:</p>
<p><a href="http://www.rphelan.com/flex/uploads/2008/09/flv-fixed.jpg"><img class="alignnone size-full wp-image-112" title="flv-fixed" src="http://www.rphelan.com/flex/uploads/2008/09/flv-fixed.jpg" alt="" width="500" height="478" /></a></p>
<p>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?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rphelan.com/2008/09/22/flvplayback-memory-leak-fixed/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
