<?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>Anna Filina &#187; garbage collection</title>
	<atom:link href="http://annafilina.com/blog/tag/garbage-collection/feed/" rel="self" type="application/rss+xml" />
	<link>http://annafilina.com/blog</link>
	<description>I fix stuff</description>
	<lastBuildDate>Wed, 04 Jan 2012 14:56:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>When garbage collection does more than you want</title>
		<link>http://annafilina.com/blog/garbage-collection-does-too-much/</link>
		<comments>http://annafilina.com/blog/garbage-collection-does-too-much/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 21:04:12 +0000</pubDate>
		<dc:creator>Anna</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[garbage collection]]></category>

		<guid isPermaLink="false">http://annafilina.com/blog/?p=325</guid>
		<description><![CDATA[I had an interesting Flex bug to solve a day before the project deadline. I was showing a progress bar while some images were preloading in the background. The application was only allowed to fire applicationComplete event once these images have finished loading.
The client complained that the progress bar stalls the first time you run the<div><a href="http://annafilina.com/blog/garbage-collection-does-too-much/">Read the rest...</a></div><br />]]></description>
			<content:encoded><![CDATA[<p>I had an interesting Flex bug to solve a day before the project deadline. I was showing a progress bar while some images were preloading in the background. The application was only allowed to fire applicationComplete event once these images have finished loading.</p>
<p>The client complained that the progress bar stalls the first time you run the app in the browser. Refreshing the page causes the application to start immediately, as if everything has been loaded. I reproduced the bug with bandwidth throttling. That made me think: &#8220;the assets are being fully loaded, but the progress bar doesn&#8217;t seem to care&#8221;.</p>
<p>With further investigation, I realized that I solved the same problem over a year ago. When one creates Loaders on the fly, such as in a loop, one often does not keep any reference to these Loaders outside of the scope of the function. Since it is a good habit to make event listeners weak, it is possible for the garbage collector to flag these Loaders for removal before they even have a chance to fire their complete event.</p>
<p>So to make sure that the progress bar does not stall, I needed to keep a reference to the Loader that I create within my loop. I simply pushed every Loader to an array. Once everything finished loading and the application started, I cleared the array to allow the garbage collector to take care of the rest.</p>
<h3>Problem</h3>
<p>Progress bar stalls while preloading images.</p>
<h3>Cause</h3>
<p>Loaders with weak event listeners and no references to them within the application.</p>
<h3>Solution</h3>
<p>Use an array to store references to the Loaders until all of them have finished loading.</p>
]]></content:encoded>
			<wfw:commentRss>http://annafilina.com/blog/garbage-collection-does-too-much/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Application Domain and External SWF Loading</title>
		<link>http://annafilina.com/blog/application-domain-and-external-swf-loading/</link>
		<comments>http://annafilina.com/blog/application-domain-and-external-swf-loading/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 10:35:36 +0000</pubDate>
		<dc:creator>Anna</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[garbage collection]]></category>

		<guid isPermaLink="false">http://annafilina.com/blog/?p=244</guid>
		<description><![CDATA[Why do we care about application domains? I worked on a project that required me to load and unload SWF files into a bigger application (loading mini-games into a virtual world). Every time I loaded a mini-game, it would never unload when I exited it. That will keep accumulating until the app slows down to<div><a href="http://annafilina.com/blog/application-domain-and-external-swf-loading/">Read the rest...</a></div><br />]]></description>
			<content:encoded><![CDATA[<p>Why do we care about application domains? I worked on a project that required me to load and unload SWF files into a bigger application (loading mini-games into a virtual world). Every time I loaded a mini-game, it would never unload when I exited it. That will keep accumulating until the app slows down to a crawl and crash because we&#8217;re out of memory.</p>
<p>The application domain and definition loading is one of the things that is not documented in a way the common mortal can understand. By popular demand, I will attempt to explain it the way I understand it. I will provide some examples along the way. Before I continue, here is a link to the official documentation on <a href="http://livedocs.adobe.com/flex/3/html/help.html?content=18_Client_System_Environment_5.html">ApplicationDomain</a>.</p>
<p>I will explain class definitions and domains, and then tell you what you can do to properly unload your movies and assets.</p>
<p><strong>Class Definitions</strong></p>
<p>Everything that you load has to be stored somewhere. It&#8217;s easy to understand the visual elements because you can just stick them on the stage. But what about class definitions?</p>
<p><span id="more-244"></span></p>
<p>For all of you who aren&#8217;t sure what class definitions are, remember all the classes that you import on top of the document when writing AS3. You can then instantiate your mx.controls.Button or flash.events.Event in the document. You can then add instances of visual components to the display list (stage).</p>
<p>Now think about your SWF for a minute. When you create a symbol in the library and export it for ActionScript, you&#8217;re basically creating a definition in the SWF. That definition will be loaded into some domain and you can instantiate it on the stage.</p>
<p>If you look at the schema, you&#8217;ll see all the class definitions in the current domain. When you close the application, all instances will be destroyed, class definitions removed and memory freed. If you decide to load an external SWF, you can choose where to load the definitions: in the same box as Class1 or in its own box.</p>
<p><strong>ApplicationDomain.currentDomain</strong></p>
<p><span style="text-decoration: underline;"><img class="aligncenter size-full wp-image-250" title="ApplicationDomain1" src="http://annafilina.com/blog/wp-content/uploads/2009/04/app-domain2.png" alt="" width="376" height="292" /><br />
</span></p>
<p>If you choose to load them in the same box as Class1, what will happen when you decide to unload your SWF? Any instances on that stage are destroyed, any definitions in its box are removed and memory is freed. Oh wait, there were no definitions in its box, so there&#8217;s nothing to remove from it. The file is unloaded in any case, but the definitions live on as a parasite in the box of our application. That&#8217;s right, a parasite. You can&#8217;t target a specific definition to unload. If you want to unload ClassC, you&#8217;ll need to unload the whole app (basically just close it). That&#8217;s not what you want to do. You want your app to run all day.</p>
<p>What is the solution? Remember there are 2 choices when loading your SWF. What if you want your loaded SWF to keep it&#8217;s garbage to itself? Sure.</p>
<p><strong>New ApplicationDomain</strong></p>
<p>So instead of loading you SWF into the application&#8217;s domain, load it into its own domain. How?</p>
<pre class="brush:as3">var appDomain:ApplicationDomain = new ApplicationDomain();
var context:LoaderContext = new LoaderContext(false, appDomain);
var loader:Loader = new Loader();
loader.load(new URLRequest("myButtons.swf"), context);</pre>
<p>We created a new domain and loaded all definitions into it. So now our application has 2 domains: the default one and a new one. You can create as many new domains as you have SWFs to load.</p>
<p><img class="aligncenter size-full wp-image-249" title="ApplicationDomain2" src="http://annafilina.com/blog/wp-content/uploads/2009/04/app-domain1.png" alt="" width="376" height="329" /></p>
<p>What will happen now if you unload your SWF? All classes are no longer in the same box, and the Loader that loaded our SWF has a reference to our new domain.  That means that when you unload your SWF, the domain (box with definitions A, B and C) will be removed and memory freed. Yay!</p>
<p><strong>Conclusion</strong></p>
<p>So what does that all mean? Should we create a new domain for EVERY file that we load? Sure you can. But you shouldn&#8217;t. There are some nice functions such as getDefinitionByName() that I want to be able to use without searching for the domain in which my definition is contained. If I have a mini-game that requires 20 SWF files, searching for that little symbol in all these domains can be painful. I would rather create 1 new domain (say domainA) and load all 20 SWFs into it. Then I can access my symbol definition from domainA, and unload this domain when no longer needed. For a second mini-game, I&#8217;ll create domainB. The idea is not to stick everything into the application&#8217;s domain.</p>
<p>Please tell me if this post helped you with your memory usage. Also, feel free to add to my explanation if I missed something.</p>
]]></content:encoded>
			<wfw:commentRss>http://annafilina.com/blog/application-domain-and-external-swf-loading/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Weak Event Listeners Warning</title>
		<link>http://annafilina.com/blog/weak-event-listeners-warning/</link>
		<comments>http://annafilina.com/blog/weak-event-listeners-warning/#comments</comments>
		<pubDate>Sat, 15 Nov 2008 22:15:53 +0000</pubDate>
		<dc:creator>Anna</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[garbage collection]]></category>
		<category><![CDATA[listeners]]></category>

		<guid isPermaLink="false">http://annafilina.com/blog/?p=122</guid>
		<description><![CDATA[An object that has no references to it will be eventually garbage-collected. An event listener is also a reference, thus preventing it from being removed from memory.
In order to allow your objects to be garbage-collected, you can either remove all listeners from it or use weak event listeners instead (don&#8217;t forget to remove other references<div><a href="http://annafilina.com/blog/weak-event-listeners-warning/">Read the rest...</a></div><br />]]></description>
			<content:encoded><![CDATA[<p>An object that has no references to it will be eventually <a href="http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html">garbage-collected</a>. An event listener is also a reference, thus preventing it from being removed from memory.</p>
<p>In order to allow your objects to be garbage-collected, you can either remove all listeners from it or use weak event listeners instead (don&#8217;t forget to remove other references to it as well). To make a listener weak, you must set the 5th parameter  of the addEventListener method to true, like this:</p>
<pre class="brush:as3">var btn:Button = new Button();
btn.addEventListener(MouseEvent.CLICK, clickHandler, false, 0, true);</pre>
<p>But you have to be very careful where you use weak event listeners. When you instantiate an object in a function&#8217;s scope and then add weak event listeners, your object could be garbage-collected before it had a chance to dispatch these events. Example:</p>
<pre class="brush:as3">private function getStuff():void {
    var service:HTTPService = new HTTPService();
    service.url = "get-stuff.php";
    service.addEventListener(ResultEvent.RESULT, resultHandler, false, 0, true);
}</pre>
<p>In this case, you should either avoid using weak listeners and remove them manually, or add a reference to your object outside of the function&#8217;s scope to prevent it from being garbage-collected too early.</p>
]]></content:encoded>
			<wfw:commentRss>http://annafilina.com/blog/weak-event-listeners-warning/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Garbage collection with SWF loading</title>
		<link>http://annafilina.com/blog/garbage-collection-with-swf-loading/</link>
		<comments>http://annafilina.com/blog/garbage-collection-with-swf-loading/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 00:47:26 +0000</pubDate>
		<dc:creator>Anna</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[garbage collection]]></category>

		<guid isPermaLink="false">http://annafilina.com/blog/archives/38</guid>
		<description><![CDATA[I ran some performance tests today. I made an interesting discovery. When you load a SWF that has elements exported for ActionScript, the class definitions of these elements are stored in the application&#8217;s domain and take up memory (we already knew that). But whether you load these definitions into the SWF&#8217;s context or your main<div><a href="http://annafilina.com/blog/garbage-collection-with-swf-loading/">Read the rest...</a></div><br />]]></description>
			<content:encoded><![CDATA[<p>I ran some performance tests today. I made an interesting discovery. When you load a SWF that has elements exported for ActionScript, the class definitions of these elements are stored in the application&#8217;s domain and take up memory (we already knew that). But whether you load these definitions into the SWF&#8217;s context or your main application&#8217;s context will determine whether that memory is ever released.</p>
<p>I tried loading SWFs using the Loader class and unloading them immediately after the load completes. These SWFs contained symbols that were exported for ActionScript (linkage). When providing a LoaderContext with its applicationDomain property set to ApplicationDomain.currentDomain, even if you unload the SWF, memory of that symbol definition is never released. However, if you do not provide one, when you unload the SWF, memory is released.</p>
<p>Conclusion: if you are to use linked library symbols in your projects, avoid loading it in the application&#8217;s domain for performance reasons. In one of my projects, I used it intensely and my application lagged and crashed after a few hours of use (could be much less depending on the size of the symbols that you have).</p>
]]></content:encoded>
			<wfw:commentRss>http://annafilina.com/blog/garbage-collection-with-swf-loading/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

