<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Chris Lyon's WebLog : Orcas</title><link>http://blogs.msdn.com/clyon/archive/tags/Orcas/default.aspx</link><description>Tags: Orcas</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>New In Orcas Part 3: GC Latency Modes</title><link>http://blogs.msdn.com/clyon/archive/2007/03/12/new-in-orcas-part-3-gc-latency-modes.aspx</link><pubDate>Mon, 12 Mar 2007 22:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1867061</guid><dc:creator>clyon</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/clyon/comments/1867061.aspx</comments><wfw:commentRss>http://blogs.msdn.com/clyon/commentrss.aspx?PostID=1867061</wfw:commentRss><description>&lt;P&gt;As you may know, there are different GC modes to choose from depending on the type of application you’re using:&amp;nbsp; Server GC, Workstation GC, and Concurrent GC (&lt;A class="" href="http://blogs.msdn.com/clyon/archive/2004/09/08/226981.aspx" mce_href="http://blogs.msdn.com/clyon/archive/2004/09/08/226981.aspx"&gt;more info&lt;/A&gt;).&amp;nbsp; These settings are process-wide, set at the beginning of the process.&amp;nbsp; Once the GC mode is set, it cannot be changed.&amp;nbsp; &lt;BR&gt;In Orcas, we’ve added the concept of GC Latency Modes that while process-wide, can be changed during the lifetime of the process to meet an application’s needs.&lt;BR&gt;The Latency Modes can be accessed as new properties onto the GCSettings class:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;CODE&gt;System.Runtime.GCLatencyMode System.Runtime.GCSettings.LatencyMode { get; set; } &lt;/CODE&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The values for GCLatencyMode are Batch, Interactive and LowLatency.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Batch:&amp;nbsp; This mode is designed for maximum throughput, at the expense of responsiveness.&amp;nbsp;&amp;nbsp; It is best for applications with no UI or server-side operations and is equivalent to Workstation GC without Concurrent GC.&amp;nbsp;&amp;nbsp; If Concurrent GC is enabled, switching to Batch mode will prevent any further concurrent collections.&amp;nbsp; This is the only valid mode for Server GC.&lt;/LI&gt;
&lt;LI&gt;Interactive:&amp;nbsp; This mode balances responsiveness with throughput.&amp;nbsp; It is designed for applications with UI and is the default Latency Mode, equivalent to Workstation GC with Concurrent GC.&amp;nbsp;&amp;nbsp; This mode is not available on Server GC.&amp;nbsp;&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;LowLatency:&amp;nbsp; This mode is meant for short-term, time-sensitive operations where interruptions from the GC may be disruptive, like animation rendering or data acquisition functions.&amp;nbsp; This mode is not available on Server GC.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;How does LowLatency mode work?&lt;/P&gt;
&lt;P&gt;When you set the latency mode to LowLatency, the GC will perform almost no generation 2 collections, nor will it start any new concurrent collections.&amp;nbsp; Since generation 2 is unbounded and can become very large, collecting it can cause your managed threads to pause for short amounts of time.&amp;nbsp; This can be unacceptable for certain scenarios.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&lt;EM&gt;To be clear, I’m not talking about real-time application requirements, rather requirements that a short-running block of code run smoothly with minimal interruptions from the runtime.&amp;nbsp; LowLatency mode is not real-time mode.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;I mentioned above that in LowLatency mode, the GC will perform almost no most generation 2 collections, but there are situations when it will.&amp;nbsp; As we know, there are three things that cause the GC to perform a collection (&lt;A class="" href="http://blogs.msdn.com/maoni/archive/2004/06/15/156626.aspx" mce_href="http://blogs.msdn.com/maoni/archive/2004/06/15/156626.aspx"&gt;more info&lt;/A&gt;):&lt;BR&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Allocation exceeds the Gen0 threshold – normally, these collections can escalate into full heap collections.&amp;nbsp; With LowLatency, generation 1 is the maximum generation that will be collected, possibly promoting objects to generation 2.&lt;/LI&gt;
&lt;LI&gt;System.GC.Collect is called – this will continue to work as expected.&amp;nbsp; If you specify to collect generation 2, the GC will honor your request regardless of the Latency Mode.&lt;/LI&gt;
&lt;LI&gt;System is in low memory situation – the OS has raised an event telling the runtime that it is low on system memory.&amp;nbsp; In this ase the GC will perform a generation 2 collection to attempt to free memory.&amp;nbsp; The alternative is to allow the OS to begin paging which will generally have worse pause times than a full collection.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;How to safely use LowLatency mode&lt;/P&gt;
&lt;P&gt;As you might have guessed, since generation 2 is rarely collected, OutOfMemoryExceptions are more likely under LowLatency mode.&amp;nbsp; Here are some guidelines to follow to avoid potential problems:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Keep the amount of time spent in LowLatency as short as possible.&amp;nbsp; Remember, you’re changing the behavior of the GC, which can lead to sub-optimal performance in the long-run.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;While in LowLatency mode, minimize the number of allocations you make, in particular allocations onto the Large Object Heap and pinned objects.&lt;/LI&gt;
&lt;LI&gt;Be mindful of other threads that could be allocating.&amp;nbsp; Remember, these settings are process-wide, so you could generate an OutOfMemoryException on any thread that may be allocating.&lt;/LI&gt;
&lt;LI&gt;Wrap the LowLatency code in a CER (&lt;A class="" href="http://blogs.msdn.com/bclteam/archive/2005/06/14/429181.aspx" mce_href="http://blogs.msdn.com/bclteam/archive/2005/06/14/429181.aspx"&gt;more info&lt;/A&gt;).&lt;/LI&gt;
&lt;LI&gt;Remember to set the latency mode back to avoid hard-to debug OutOfMemoryExceptions later.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Here’s a code sample of how to use LowLatency mode&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;CODE&gt;
&lt;P&gt;// preallocate objects here&lt;BR&gt;GCLatencyMode oldMode = GCSettings.LatencyMode;&lt;BR&gt;RuntimeHelpers.PrepareConstrainedRegions();&lt;BR&gt;try&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; GCSettings.LatencyMode = GCLatencyMode.LowLatency;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // perform time-sensitive actions here&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; minimize:&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; -all allocations, especially LOH allocations&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; -pinning&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; -allocations on other threads&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; */&lt;BR&gt;}&lt;BR&gt;catch (ApplicationException)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // catch any exceptions you expect your application to throw&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // perform cleanup code&lt;BR&gt;}&lt;BR&gt;finally&lt;BR&gt;{&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // always set the mode back!&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; GCSettings.LatencyMode = oldMode; &lt;BR&gt;}&lt;/P&gt;&lt;/CODE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Remember, this mode can cause failures in your application, so please use good judgment when using it.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1867061" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/clyon/archive/tags/GC/default.aspx">GC</category><category domain="http://blogs.msdn.com/clyon/archive/tags/Orcas/default.aspx">Orcas</category></item><item><title>New In Orcas Part 2: GC Collection Modes</title><link>http://blogs.msdn.com/clyon/archive/2007/03/07/new-in-orcas-part-2-gc-collection-modes.aspx</link><pubDate>Thu, 08 Mar 2007 01:14:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1831076</guid><dc:creator>clyon</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/clyon/comments/1831076.aspx</comments><wfw:commentRss>http://blogs.msdn.com/clyon/commentrss.aspx?PostID=1831076</wfw:commentRss><description>&lt;P&gt;In Orcas we’ve added an overload to System.GC.Collect():&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;CODE&gt;void System.GC.Collect(int generation, System.GCCollectionMode mode)&lt;/CODE&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Where generation is the highest generation to collect (from 0 to System.GC.MaxGeneration) and mode can be:&lt;BR&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Default:&amp;nbsp; the same behavior if you called GC.Collect without specifying the mode.&amp;nbsp; Currently this is the same behavior as Forced, but this is subject to change in future versions of the runtime.&lt;/LI&gt;
&lt;LI&gt;Forced:&amp;nbsp; guarantees a collection occurs for all the generations up to and including generation.&amp;nbsp; &lt;/LI&gt;
&lt;LI&gt;Optimized:&amp;nbsp; this mode will tell the GC to only collect if it determines that a collection will be productive.&amp;nbsp; In this case, “productive” is determined by a number of factors, including amount of memory considered garbage, heap fragmentation, etc.&amp;nbsp; The exact formula is subject to change between CLR releases.&amp;nbsp; If the GC decides a collection will not be productive, then the call will have no effect.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;When should we use these new modes?&lt;/P&gt;
&lt;P&gt;Calling GC.Collect is generally discouraged, but as Rico points out &lt;A class="" href="http://blogs.msdn.com/ricom/archive/2004/11/29/271829.aspx" mce_href="http://blogs.msdn.com/ricom/archive/2004/11/29/271829.aspx"&gt;here&lt;/A&gt;, there are legitimate circumstances where you know there is a large number of objects you’ll never need again.&amp;nbsp; For example, at the end of a game level, when a custom Form is closed, or when a web form is finished, there may be a number of long-lived objects in generation 2 that are now dead.&amp;nbsp; By calling an Optimized collection at this point you give the GC a chance to evaluate the heap, and have it decide if a collection will free enough memory to be worth it. &lt;/P&gt;
&lt;P&gt;Forced and Default modes should generally only be used for debugging or testing scenarios, where you want to ensure objects are collected at a certain point in your application, or want to compare performance data.&amp;nbsp; In future versions of the CLR, there may be new guidance for using Default, but for now, its use is discouraged.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1831076" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/clyon/archive/tags/GC/default.aspx">GC</category><category domain="http://blogs.msdn.com/clyon/archive/tags/Orcas/default.aspx">Orcas</category></item><item><title>New In Orcas Part 1: What we’ve been doing</title><link>http://blogs.msdn.com/clyon/archive/2007/03/05/new-in-orcas-part-1-what-we-ve-been-doing.aspx</link><pubDate>Tue, 06 Mar 2007 08:14:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1814520</guid><dc:creator>clyon</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/clyon/comments/1814520.aspx</comments><wfw:commentRss>http://blogs.msdn.com/clyon/commentrss.aspx?PostID=1814520</wfw:commentRss><description>&lt;P&gt;The &lt;A class="" href="http://www.microsoft.com/downloads/details.aspx?familyid=281fcb3d-5e79-4126-b4c0-8db6332de26e&amp;amp;displaylang=en" mce_href="http://www.microsoft.com/downloads/details.aspx?familyid=281fcb3d-5e79-4126-b4c0-8db6332de26e&amp;amp;displaylang=en"&gt;Orcas March CTP&lt;/A&gt;&amp;nbsp;is out, and what does that mean for the Garbage Collector?&amp;nbsp; The GC team has been concentrating on three areas for this release:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Bug fixes.&amp;nbsp; For Orcas, we’ve fixed several premature Out of Memory bugs, improved stability in certain stressful conditions, and even improved performance in some scenarios.&amp;nbsp;&amp;nbsp; &lt;BR&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A class="" href="http://blogs.msdn.com/clyon/archive/2007/03/07/new-in-orcas-part-2-gc-collection-modes.aspx" mce_href="http://blogs.msdn.com/clyon/archive/2007/03/07/new-in-orcas-part-2-gc-collection-modes.aspx"&gt;GC Collection Modes&lt;/A&gt;.&amp;nbsp; We’ve added a new overload to System.GC.Collect that takes a System.GCCollectionMode enum, which allows the user to either force a collection or allow the GC to decide if a collection would be productive.&lt;BR&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A class="" href="http://blogs.msdn.com/clyon/archive/2007/03/12/new-in-orcas-part-3-gc-latency-modes.aspx" mce_href="http://blogs.msdn.com/clyon/archive/2007/03/12/new-in-orcas-part-3-gc-latency-modes.aspx"&gt;GC Latency Modes&lt;/A&gt;.&amp;nbsp; A new GC feature that allows the user to specify blocks of code that are time-sensitive, and the GC will minimize its intrusiveness.&amp;nbsp;&amp;nbsp; This has been implemented as new properties on the System.Runtime.GCSettings class.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;In my next few blog entries I’ll go into more detail on our new features, including code samples and best practices.&lt;BR&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1814520" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/clyon/archive/tags/GC/default.aspx">GC</category><category domain="http://blogs.msdn.com/clyon/archive/tags/Orcas/default.aspx">Orcas</category></item></channel></rss>