<?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>Olhovsky &#187; garbage collection</title>
	<atom:link href="http://www.olhovsky.com/tag/garbage-collection/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.olhovsky.com</link>
	<description>Programming, meet art.</description>
	<lastBuildDate>Mon, 23 Jan 2012 05:19:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Foreach through non-primitive types creates garbage.</title>
		<link>http://www.olhovsky.com/2009/09/code-common-to-many-xna-examples-generates-unneccesary-garbage/</link>
		<comments>http://www.olhovsky.com/2009/09/code-common-to-many-xna-examples-generates-unneccesary-garbage/#comments</comments>
		<pubDate>Sat, 19 Sep 2009 02:44:42 +0000</pubDate>
		<dc:creator>olhovsky</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[garbage collection]]></category>

		<guid isPermaLink="false">http://www.olhovsky.com/?p=81</guid>
		<description><![CDATA[Time and again I have seen code like this used in XNA tutorials. effectDrawBlock.Begin(); foreach (EffectPass pass in effectDrawBlock.CurrentTechnique.Passes) { pass.Begin(); gd.DrawIndexedPrimitives(PrimitiveType.TriangleStrip, 0, 0, 35, 0, 70); pass.End(); } effectDrawBlock.End(); When you use a foreach over an array of ints &#8230; <a href="http://www.olhovsky.com/2009/09/code-common-to-many-xna-examples-generates-unneccesary-garbage/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Time and again I have seen code like this used in XNA tutorials.</p>
<pre class="brush:c#">effectDrawBlock.Begin();
foreach (EffectPass pass in effectDrawBlock.CurrentTechnique.Passes)
{
    pass.Begin(); 

    gd.DrawIndexedPrimitives(PrimitiveType.TriangleStrip, 0, 0, 35, 0, 70); 

    pass.End();
}
effectDrawBlock.End();</pre>
<p>When you use a foreach over an array of ints (or any value type), no garbage is generated, it&#8217;s fast, readable, and often preferable to a for loop.</p>
<p>However, in the above code, &#8220;EffectPass pass in&#8221; creates a managed effect pass object, used to iterate through the collection. That object then needs to be handled by the garbage collector later.</p>
<p>The fix is to use a for loop when iterating over non-primitive types.<br />
In my case, by changing my code to something more like what you see below, I was able to reduce the number of managed objects generated by my code from 2500/sec to 200/sec as measured by the XNA Framework Remote Perf Monitor.</p>
<pre class="brush:c#">effectDrawBlock.Begin();
for (int i = 0; i &lt; effectDrawBlock.CurrentTechnique.Passes.Count; i++)
{
    effectDrawBlock.CurrentTechnique.Passes[i].Begin();

    gd.DrawIndexedPrimitives(PrimitiveType.TriangleStrip, 0, 0, 35, 0, 70);

    effectDrawBlock.CurrentTechnique.Passes[i].End();
}
effectDrawBlock.End();</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.olhovsky.com/2009/09/code-common-to-many-xna-examples-generates-unneccesary-garbage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

