<?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>Teaching an old dog new tricks: GC fun in Whidbey</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx</link><description>The GC does an amazing job of managing managed memory… We really work hard to make sure pages stay hot, and the memory is cached where you need it…. But the reality of the world is that for many folks it is not just managed memory they have to deal with.</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>RE: Teaching an old dog new tricks: GC fun in Whidbey</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#50949</link><pubDate>Sun, 14 Dec 2003 10:41:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:50949</guid><dc:creator>MartinJ</dc:creator><description>I would love to see managed code be informed when the GC is about to kick off a collection.  Or, some way to determine when memory is starting to run low.  That way, I could implement my own form of data caching that can give up references when things get tight.

I realize that someone might abuse this ability to make the GC deterministic.  But, the benefits could allow for applications that don't become such memory hogs.

-MartinJ</description></item><item><title>RE: Teaching an old dog new tricks: GC fun in Whidbey</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#50950</link><pubDate>Mon, 15 Dec 2003 00:08:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:50950</guid><dc:creator>Dejan Jelovic</dc:creator><description>Brad,

HandleCollector is a half-assed fix that will simply not fix the issue:

1. GC is asynchrnous and there are plenty of ways to run out of handles before GC has time to free them through finalization.

2. It's expensive, as forces collection along both dimensions (resources and memory) even though only one may be reaching its limits.

But you guys are writing a new operating system. Why not dispense with archaic limits? There is no reason for pens, brushes, bitmaps and most other GDI (plus a few User) resources to be limited by anything but the memory they take up. (Fonts are probably an exception.)

Also, some resource management patterns need to be built into the framework and checked by FxCop so that scew-ups like the one I described at http://www.jelovic.com/weblog/e119.htm don't happen too often.

Dejan</description></item><item><title>RE: Teaching an old dog new tricks: GC fun in Whidbey</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#50951</link><pubDate>Sat, 20 Dec 2003 20:23:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:50951</guid><dc:creator>Jeremy Spiller</dc:creator><description>I think there is a large potential for really messing the system up by mismatching calls to GC.AddMemoryPressure and GC.RemoveMemoryPressure.  It would be *very* hard to figure out where the problem is.  How about a single member function (maybe you'd have to inherit from some base object):

this.SetMemoryPressure(int n)

Now the GC knows how much memory the object is using at all times, and there is no chance of a library (someone else's, of course:) messing up the system.

In addition, the GC might use the information to call a finalizer earlier than it would otherwise.  Maybe the GC could &amp;quot;keep an eye&amp;quot; on huge objects.

-Jeremy</description></item><item><title>re: Teaching an old dog new tricks: GC fun in Whidbey</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#93356</link><pubDate>Sun, 21 Mar 2004 02:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:93356</guid><dc:creator>Justin Rogers</dc:creator><description>First comment, does .AddMemoryPressure work on a global level (adding more maximum memory consumed), or does it attach this information to the instance in question.  I'm assuming you might grab the class instance and assign the new memory pressure value to the class itself under the covers, but I want to make sure.&lt;br&gt;&lt;br&gt;Does .AddMemoryPressure scale to inheritance situations where a parent object (Bitmap), might set pressure based on the file size and then say a TwoLayerBitmap would get the chance to add additional pressure based on some secondary unmanaged buffers it creates?&lt;br&gt;&lt;br&gt;Memory pressure can get removed during the Dispose method or during Finalization.  I'm assuming that you would need to do a RemoveMemoryPressure during a Dispose/Close operation, but during a Finalize, the GC should assume a RemoveMemoryPressure?&lt;br&gt;&lt;br&gt;As for the HandleCollector, is there anything this is buying us that we haven't already done ourselves as .NET programmers?  Is this just a helper class that handles calling the GC for us?  Does it force the GC to only collect specific types of objects?  It doesn't appear so based on the code.</description></item><item><title>The new face of the GC in Whidbey...  I'm not sure this is a pretty face...</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#93359</link><pubDate>Sun, 21 Mar 2004 05:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:93359</guid><dc:creator>Justin Rogers, DigiTec Web Consultants, LLC.</dc:creator><description /></item><item><title>Discussion on GC in Whidbey</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#94326</link><pubDate>Tue, 23 Mar 2004 08:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:94326</guid><dc:creator>Brad Abrams </dc:creator><description /></item><item><title>re: Teaching an old dog new tricks: GC fun in Whidbey</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#94380</link><pubDate>Tue, 23 Mar 2004 07:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:94380</guid><dc:creator>mihailik</dc:creator><description>Now Add/RemoveMemoryPressure don't attach the pressure amount to object instance. I don't know why. This 'agnostic' style really will produce bugs.&lt;br&gt;&lt;br&gt;What can be done? Just we need is wrapper base class, that will automatically ensure AddPressure/RemovePressure called in right scenario. I think, MS must ship such class in Widbey to prevent our bugs.&lt;br&gt;&lt;br&gt;Below is my vision. Just automatically Add in ctor and Remove in Dispose.&lt;br&gt;&lt;br&gt;This code is just simple demo. It need be tuned for locking/threading and for checking Add/Remove pair matches.&lt;br&gt;&lt;br&gt;public abstract class UnmanagedResource : IDisposable&lt;br&gt;{&lt;br&gt;    readonly int m_PressureAmount;&lt;br&gt;&lt;br&gt;    public UnmanagedResource(int pressureAmount)&lt;br&gt;        : this( pressureAmount, true )   &lt;br&gt;    { }&lt;br&gt;&lt;br&gt;&lt;br&gt;    public UnmanagedResource(int pressureAmount, bool addPressureNow)&lt;br&gt;    {&lt;br&gt;        this.m_PressureAmount=pressureAmount;&lt;br&gt;        if( addPressureNow )&lt;br&gt;            ResourceAllocated();&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    protected void ResourceAllocated()&lt;br&gt;    {&lt;br&gt;        GC.AddMemoryPressure(PressureAmount);&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    protected void ResourceReleased()&lt;br&gt;    {&lt;br&gt;        GC.RemoveMemoryPressure(PressureAmount);&lt;br&gt;    }&lt;br&gt;&lt;br&gt;&lt;br&gt;    protected int PressureAmount&lt;br&gt;    {&lt;br&gt;        get { return m_PressureAmount; }&lt;br&gt;    }&lt;br&gt;&lt;br&gt;&lt;br&gt;    public void Dispose()&lt;br&gt;    {&lt;br&gt;        Dispose(true);&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    protected virtual void Dispose(bool disposing)&lt;br&gt;    {&lt;br&gt;        ResourceReleased();&lt;br&gt;&lt;br&gt;        if( disposing )&lt;br&gt;        {&lt;br&gt;            GC.SuppressFinalize();&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    ~UnmanagedResource()&lt;br&gt;    {&lt;br&gt;        Dispose(false);        &lt;br&gt;    }&lt;br&gt;}</description></item><item><title>re: Teaching an old dog new tricks: GC fun in Whidbey</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#94412</link><pubDate>Tue, 23 Mar 2004 09:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:94412</guid><dc:creator>Yves Reynhout</dc:creator><description>Not directly related, but has the GC version (srv and wks) been changed for Windows Services? I mean, if there is one place where we really need the server version it's in Windows Services!</description></item><item><title>[.NET]Teaching an old dog new tricks: GC fun in Whidbey</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#94429</link><pubDate>Tue, 23 Mar 2004 13:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:94429</guid><dc:creator>おがわみつぎのBLOG</dc:creator><description>[.NET]Teaching an old dog new tricks: GC fun in Whidbey</description></item><item><title>re: Teaching an old dog new tricks: GC fun in Whidbey</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#94491</link><pubDate>Tue, 23 Mar 2004 13:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:94491</guid><dc:creator>Brad Abrams</dc:creator><description>Yves, the runtime will choose what GC to load based on the machine we are running on... Multiproc get the server GC for example.  You can also customize it on each process via a config file</description></item><item><title>Items of interest pt6</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#95099</link><pubDate>Wed, 24 Mar 2004 10:50:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:95099</guid><dc:creator>Andrew Stopford's Weblog</dc:creator><description /></item><item><title>Items of interest pt6</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#95103</link><pubDate>Wed, 24 Mar 2004 10:51:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:95103</guid><dc:creator>Andrew Stopford's Weblog</dc:creator><description /></item><item><title>re: Teaching an old dog new tricks: GC fun in Whidbey</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#95789</link><pubDate>Thu, 25 Mar 2004 06:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:95789</guid><dc:creator>Flier Lu</dc:creator><description>    Why not use IoC pattern connected the managed object and the unmanaged resource? For example, define an interface IUnmanagedResource, which support GC got the unmanaged resource size with IUnmanagedResource.GetUnmanagedResourceSize function.&lt;br&gt;&lt;br&gt;public interface IUnmanagedResource&lt;br&gt;{&lt;br&gt;  uint GetUnmanagedResourceSize();&lt;br&gt;};&lt;br&gt;&lt;br&gt;    The managed object implement the interface, and register itself to GC UnmanagedResource list with GC.RegisterForUnmanagedResource, like GC Finalizer list.&lt;br&gt;&lt;br&gt;// GC.RegisterForUnmanagedResource(IUnmanagedResource res)&lt;br&gt;&lt;br&gt;class Bitmap : IUnmanagedResource&lt;br&gt;{&lt;br&gt;   private long _size;&lt;br&gt;&lt;br&gt;   uint GetUnmanagedResourceSize()&lt;br&gt;   {&lt;br&gt;     return _size;&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   Bitmap (string path )&lt;br&gt;   {&lt;br&gt;      _size = new FileInfo(path).Length;&lt;br&gt;&lt;br&gt;      GC.RegisterForUnmanagedResource(this);&lt;br&gt;&lt;br&gt;      // other work&lt;br&gt;   }&lt;br&gt;}&lt;br&gt;    So GC could calculate the unmanaged size, and known which unreachable object has how many unmanaged resource.</description></item><item><title>re: Teaching an old dog new tricks: GC fun in Whidbey</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#96215</link><pubDate>Thu, 25 Mar 2004 18:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:96215</guid><dc:creator>Robin Maffeo</dc:creator><description>A minor correction to Brad's comment on wks/svr GC selection...  As he mentioned, we added a new config flag in Whidbey and v1.1 SP1 where you can choose which flavor of the collector you want.  However, absent any guidance, workstation GC is still the default.  The runtime won't automatically select server GC for you since the assumption is an interactive application unless otherwise specified.</description></item><item><title>re: Teaching an old dog new tricks: GC fun in Whidbey</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#98079</link><pubDate>Sat, 27 Mar 2004 16:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:98079</guid><dc:creator>Antonello Tesoro</dc:creator><description>Assume we've implemented the Dispose Pattern in our type which holds references to un unmanaged scarce resource.&lt;br&gt;When the HandleCollector wants to free as much of our unmanaged resource as it can ( because for instance we've reached the minThreshold ) it has no other choices but to perform a &amp;quot;Full Collection&amp;quot; so that the Finlize method of the instances of our type will be called. This carries on the undesired consequences we're all well aware of, such as incorrect Generation promotion of some objetcs, the performance penalty of a full GC collection, the Finalization of objetcs of other types we didn't need to Finalize at this time; all This, just to have the Finalize method of all the live instances of our type to be called!&lt;br&gt;&lt;br&gt;What is really needed in those cases is something like:&lt;br&gt;&lt;br&gt;public static void GC.FinalizeType( Type typeToFinalize, bool runSync );&lt;br&gt;&lt;br&gt;This method should suspend all the threads and walk the managed heap using the very same algorithms the GC already uses in the standard Collection, to find out which instances of our types aren't reachable; and only call the Finalize mthod on those instances without performing a full collection or reclaiming any memory. The second flag asks the method to wait until all those objects have been fully finalized ( this is needed if we  really ran out of resources and cannot continue the execution of our program without freing any ).&lt;br&gt;I Belive this kind of solution will get the same result of the GC.Collect without the side effects and the performance penalties of a full collection.</description></item><item><title>AddMemoryPressure</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#260368</link><pubDate>Thu, 18 Nov 2004 11:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:260368</guid><dc:creator>Cook Computing</dc:creator><description>Avnrao's Blog has a post GC on small managed resource holding large unmanaged resources... By default small objects will have a low priority as far as GC is concerned but in Whidbey onwards you can boost this by calling GC.AddMemoryPressure....</description></item><item><title>Performance Guideline: Use AddMemoryPressure while consuming Unmanaged Objects through COM Interop</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#810585</link><pubDate>Tue, 10 Oct 2006 02:56:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:810585</guid><dc:creator>J.D. Meier's Blog</dc:creator><description>&lt;p&gt;Here's the next .NET Framework 2.0 performance guideline for review from Prashant Bansode, Bhavin Raichura,&lt;/p&gt;
</description></item><item><title>Performance Guideline: Use HandleCollector API when Managing Expensive Unmanaged Resource Handles</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#823511</link><pubDate>Sat, 14 Oct 2006 01:20:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:823511</guid><dc:creator>J.D. Meier's Blog</dc:creator><description>&lt;p&gt;Here's the next .NET Framework 2.0 performance guideline for review from Prashant Bansode, Bhavin Raichura,&lt;/p&gt;
</description></item><item><title> Brad Abrams Teaching an old dog new tricks GC fun in Whidbey | work from home</title><link>http://blogs.msdn.com/brada/archive/2003/12/12/50948.aspx#9761271</link><pubDate>Tue, 16 Jun 2009 15:13:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9761271</guid><dc:creator> Brad Abrams Teaching an old dog new tricks GC fun in Whidbey | work from home</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://workfromhomecareer.info/story.php?id=18132"&gt;http://workfromhomecareer.info/story.php?id=18132&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>