Inside the .NET Managed Heap

Of all of the technologies to be found in .NET, the most controversial, seems to be garbage collection. A key part of the .NET framework, the managed heap and the garbage collection mechanism are foreign ideas to many of us. This installment of my .NET column discusses the managed heap, and how you can take advantage of it.

Why a Managed Heap?

The .NET framework includes a managed heap that all .NET languages use when allocating reference type objects. Lightweight objects known as value types are always allocated on the stack, but all instances of classes and arrays are created from a pool of memory known as the managed heap.

The basic algorithm used by the garbage collector is quite simple:

  • Mark all managed memory as garbage
  • Look for used memory blocks, and mark them as valid
  • Discard all unused memory blocks
  • Compact the heap

Managed Heap Optimizations

The high-level overview seems simple enough, but the actual steps taken by the garbage collector and other parts of the heap management system are not trivial, and often involve optimizations designed to improve performance. For example, a garbage collection pass of the entire memory pool can be quite expensive. However, studies show that most objects allocated from a managed heap have a very short lifetime, so the heap is divided into three sections, known as generations. Newly allocated objects are placed into generation zero, and this generation is always collected first—it is the most likely place to find some unused memory, and due to its small size (it's small enough to fit into the processor's L2 cache) a collection here is extremely fast and productive.

Another optimization found in the managed heap concerns a principle known as locality of reference. This principle says that objects allocated together are often used together. If objects can be located in close proximity in the heap, cache performance is improved. Due to the nature of the managed heap, objects are always allocated on contiguous addresses, and the heap is kept compacted, resulting in objects growing closer together, never further apart. This is in contrast to the standard heap offered to unmanaged code, where the heap can be easily fragmented, and objects allocated together are often located relatively far apart in the heap.

Still another optimization concerns large objects. In general, large objects tend to have very long lifetimes. When a large object is allocated in the .NET managed heap, it is allocated from a special portion of the heap that is never compacted. The performance hot incurred by moving a large object outweighs the performance gain found by compacting that portion of the heap.

What about External Resources?

The garbage collector efficiently handles freeing resources from the managed heap, but a collection is only initiated when memory pressure triggers a collection. What about classes that manage limited external resources such as database connections or Windows handles? Waiting until a garbage collection is triggered to clean up database connections or file handles can severely degrade system performance.

Classes that hold external resources should implement a Close or Dispose method that is called by clients when the object is no longer used. Beginning in Beta 2, the Dispose pattern is formalized via the IDisposable interface, which is discussed later in this article.

Classes that need to clean up resources should also implement a finalizer. In C#, the preferred way to create a finalizer is to implement a destructor, while at the framework level, a finalizer is created by overriding the System.Object.Finalize method. These two methods of implementing a finalizer are equivalent:

~OverdueBookLocator()
{
    Dispose(false);
}

and:

public void Finalize()
{
    base.Finalize();
    Dispose(false);
}

In C#, it is an error to implement both a Finalize method and a destructor.

You should not create a destructor or finalization routine if you don't absolutely require finalization semantics. Finalizers decrease system performance, and increase the memory pressure on the runtime. Also, due to the way that finalizers are executed you can't guarantee when or even if a finalizer will be called.

Allocation and Garbage Collection Details

With the GC overview out of the way, let's discuss the details of how allocation and collection work in the managed heap. The managed heap looks nothing like a traditional heap that you're accustomed to using in C++ programming. In a traditional heap, a data structure is used to track free chunks of memory. Searching for a block of memory of a particular size can be a time-consuming chore, especially if the heap becomes fragmented. By contrast, in the managed heap memory is arranged as a contiguous array; a pointer tracks the boundary between allocated and free memory. As memory is allocated, the pointer is simply incremented—resulting in much higher performance for allocations.

When objects are allocated, they are initially placed in generation zero. When generation zero nears its maximum size, a collection is initiated against generation zero only. This is a very fast GC pass, due to the small size of generation zero. A generation zero collection always results in generation zero being completely flushed. Any objects that are discovered to be garbage are freed, and any objects actually in use are compacted and promoted to generation one.

As generation one nears its maximum size due to the number of objects aging into it from generation zero, a collection phase will be initiated against generations zero and one. As with a generation zero collection, all unused objects will be freed, and valid objects will be compacted and moved into the next generation. Most GC passes target generation zero, which is a target-rich environment due to the number of unused temporary objects found in generation zero. Generation two collection passes are the most costly, and are only initiated when generation zero and one collections don't free enough memory. If a generation two collection pass is not able to free enough memory for an allocation, an OutOfMemoryException exception object is thrown.

Objects that require finalization complicate the collection process. When an object with a finalizer is identified as garbage, it is not immediately released. Instead it's placed into a finalization queue, which establishes a reference to the object, preventing its collection. A background thread executes the finalizer for each object, and removes the finalized object from the finalization queue. Only after finalization can the object be removed from memory during the next collection pass. As a side effect of surviving one collection pass, the object is promoted into a higher generation, further delaying its eventual collection.

Classes that require finalization should implement the IDisposable interface in order to allow client to short-circuit finalization. IDisposable includes one method—Dispose. This interface, which was introduced for Beta 2, formalizes a pattern that was in wide use even prior to Beta 2. Essentially, an object requiring finalization exposes a Dispose method. This method is expected to free external resources and suppress finalization, as shown in this typical code fragment:

public class OverdueBookLocator: IDisposable
{
    ~OverdueBookLocator()
    {
        InternalDispose(false);
    }

    public void Dispose()
    {
        InternalDispose(true);
    }

    protected void InternalDispose(bool disposing)
    {
        if(disposing)
        {
            GC.SuppressFinalize(this);
            // Dispose of managed objects if disposing.
        }
        // free external resources here
        .
        .
        .
    }
}

Note that there are two ways that the object is cleaned up. The first way is through the Dispose method from the IDisposable interface. This method is called by client code when an object is explicitly terminated; this method calls InternalDispose(true). All objects are cleaned up in this case. If the destructor is called, InternalDispose(false) is called, and only external resources are released. If we are being finalized, managed objects that we own may have already been collected, and referencing them may cause an exception to be generated.

The call to GC.SuppressFinalize prevents the garbage collector from placing the object into the finalization queue. This reduces memory pressure because the object can be freed in a single GC pass, and improves performance because the finalizer does not need to be executed.

Disposal Optimizations for C#

So using the IDisposable.Dispose() method to clean up resources is a great way to relieve some of the memory pressure place on the managed heap, and it reduces the number of objects that must undergo finalization. However, it is cumbersome to use, especially when multiple temporary objects are created. In order to properly take advantage of the IDisposable interface, a C# client would need to write code like this:

OverdueBookLocator bookLocator = null;
try
{
    bookLocator = new OverdueBookLocator();
    // Use bookLocator here
    Book book = bookLocator.Find("Eiffel, the Language");
    .
    .
    .
}
finally
{
    if(bookLocator != null)
    {
        IDisposable disp = bookLocator as IDisposable;
        disp.Dispose();
    }
}

The finally block is required so that the proper clean up is performed if an exception is thrown. In order to simplify the use of the Dispose pattern by clients, the using statement was introduced for Beta 2. The using statement allows you to simplify your code; so the earlier example can be boiled down to:

using(bookLocator = new OverdueBookLocator())
{
   // Use bookLocator here
   Book book = bookLocator.Find("Eiffel, the Language");
}

You should employ the using statement whenever you allocate types that have a well-defined lifetime. It guarantees proper handling of the IDisposable interface, even in the presence of exceptions.

Using the System.GC Class

The System.GC class is used to access the garbage collection mechanism exposed by the .NET framework. This class includes the following useful methods:

  • GC.SuppressFinalize was described earlier in the column; this method inhibits finalization for an object. Call this method if you have already released external resources owned by an object.
  • GC.Collect comes in two versions. The version that has no parameter performs a full collection on all generations in the managed heap. Another version accepts an integer value representing the generation to be collected. You'll rarely need to call this method, as the garbage collector automatically runs when needed.
  • GC.GetGeneration returns the generation number for an object passed as a parameter. This method is useful when debugging or tracing for performance reasons, but has limited value in most applications.
  • GC.GetTotalMemory returns the amount of memory allocated in the heap. This number is not exact due to the way the managed heap works, but a close approximation can be obtained if true is passed as a parameter. This causes a collection to be performed before the memory usage is calculated.

An example of an audit method that uses these functions is provided below:

/// <summary>
/// Displays current GC information
/// </summary>
/// <param name="generation">The generation to collect</param>
/// <param name="waitForGC">Run GC before calculating usage?</param>
public void CollectAndAudit(int generation, bool waitForGC)
{
  int myGeneration = GC.GetGeneration(this);
  long totalMemory = GC.GetTotalMemory(waitForGC);
  Console.WriteLine("I am in generation {0}.", myGeneration);
  Console.WriteLine("Memory before collection {0}.", totalMemory);
  GC.Collect(generation);
  Console.WriteLine("Memory after collection {0}.", totalMemory);
}

Future Columns

In my next column I'll discuss using the XSL classes in .NET and some other XML goodies.  Following that article I have plans for articles on .NET remoting, as well as articles on interop with existing code, and an article on multi-threaded programming in .NET.

About the Author

Mickey Williams is the founder of Codev Technologies, a provider of tools and consulting for Windows Developers. He is also on the staff at .NET Experts (www.dotnetexperts.com), where he teaches the .NET Framework course. He has spoken at conferences in the USA and Europe, and has written eight books on Windows programming. He currently claims to be writing "Microsoft Visual C#" for MS Press. Mickey can be reached at mw@codevtech.com.



Comments

  • wheloltabotly PumeSonee Phobereurce 3855023

    Posted by TizefaTaNaday on 06/14/2013 12:37pm

    eldesmepE cheapjordan10forsale.holidaygiving.org intonterb airjordanretro4.holidaygiving.org enastmahlen

    Reply
  • wheloltabotly PumeSonee Phobereurce 6951355

    Posted by TizefaTaNaday on 05/28/2013 03:59am

    VienAerorne http://www.zhbond.com/images/index.php?louis-vuitton-cuir-de-vachettelouis-vuittonclearance-sale-outlet.html praisaamibe http://spirittkd.co.uk/newsg/fashion.php?p=7061 logeostaskema

    Reply
  • kors watch

    Posted by aidedeAcquini on 03/13/2013 01:55pm

    about michael kors watches For some people, sporting leather may be a sign of disrespect within the animals what people died to supply us high-end items. Still, others visit leather just like a sign of achievements and michael michael kors watches capability. Either way, leather strapped running watches are very noticeably in demand right now. In particular, Eric Kors watches use a unique design that many people like. Theres the baggage shaped keep an eye on that professional sports a gear closure, just like luggage. Other kinds include a increase wrap strap, with the view face as being padlock; bright leather symbol plate different watches; and alligator or simply python strap wrist watches. For someone what individuals loves buckskin, this variety Michael Kors wristwatches are beautiful and perfect gold michael kors watch

    Reply
  • clarks shoes sale

    Posted by FlereeEffix on 03/12/2013 01:32pm

    www.clarksshoesbysale.net The makers to do with Clarks Shoes usually have believed inside Darwin theory connected with survival of your fittest! They, in addition to the just unique ideas from quality system, also handed over a lot of curiosity on to enhancing new clarks shoe styles and designs periodically- those creations which started to be benchmark in the realm of style and class. The Clarks Proper footwear took everyone by its storms by simply introducing a portion of the legendary types of shoes when Desert Shoes or boots, Clark Classic coupled with Wallabee- to name couple of that still receives a priced procession for any ardent shoe lover. The financial lending of release of polyurethane as jogging shoe sole to be sure the shoes end up being light weight and difficult wearing- goes absolutely to Clarks pair of shoes clarks shoe

    Reply
  • cambridge bag satchel

    Posted by Shoorgeenlano on 03/02/2013 11:13am

    cambridge satchels There is no need that you will eliminate the odour by utilizing together sodium bicarbonate not to mention carbon odor absorber because it is depending upon the effectiveness of reek. Which means you will not need to to determine cambridge satchels before hand about carried on the connection, right before meeting the average person on the very few conferences not to mention dates. Become lower to address and knees and run after him - permit your man to pursue you once again, but stay on both hands plus knees the cambridge bag

    Reply
  • Finding the Almost all Legitimate Sacramento Medical Marijuana Wellness Core

    Posted by Attanoboollef on 02/05/2013 11:35pm

    Cramer, bad system Latin abbreviation, (probably) possible it outlook cancer-causing agents, for example benzene. The primary thing you should do in a healthy on offer, 25-12 don't have malignancy identification card for its use. This is why they get moody, restless, anxious alcohol advantages best vaporizers is available, the states in the U.S. [url=http://vaporizerworld.org/best-vaporizer/]Portable Vaporizer[/url] They are allowed six mature plants like you with boards market parts by that they are utilizing to keep everything in place. Dispensaries have opened across the state, but experienced job $150 as payment for processing the medical marijuana ID card. Even patients, who have never tried cannabis recreationally, with They demand web, casual tests have medically valuable, others don"t.

    Reply
  • cheapnfl jerseys

    Posted by SoidaLoli on 12/28/2012 02:22pm

    Surrounding the fella might need clay matthews nike jersey your under wear, furthermore, she will get off at the proficiency involving steering clear of making use of the felony of stealing content. It's have to deal with is really a this catches a person's eye of several internet surfers however the soft cedric benson jersey packers piece which enables the purchase. Foldable pea parka jerkin, hat by means of fully extended go, dress elite jeff saturday jersey with the help of grab and also hoods are generally brand new with Women's outdoor jackets design and style developments.

    Reply
  • cheapnfl jerseys

    Posted by Sulferierfoke on 12/24/2012 06:58am

    This a valuable thing in relation to females footwear is antonio brown steeler jersey the fact there will probably be substantial colour even just in the particular formalized apply to that is why it all shows that maurkice pouncey steelers jersey variety extremely pertaining to graphic designers. By means of going through activities such as dinning establishments or maybe member's contains, you will make womens ministry more given that it game steelers keisel jersey simply just is definitely not "another" meeting.

    Reply
  • Test, just a test

    Posted by AbsowsBow on 11/16/2012 11:45pm

    Regulators license MGM Resorts' planned Web gambling subsidiary [url=http://AUSTRALIA-CASINO-POKIES.COM/]casino|[url=http://AUSTRALIA-FREE-CASINO.COM/]blackjack|[url=http://AUSTRALIA-ONLINE-CASINOS.COM/]slots|[url=http://FINLANDINTERNETCASINO.COM/]online casino[/url] LAS VEGAS -- MGM Resorts Global's planned online gaming subsidiary was licensed Thursday next to the Nevada Gaming Commission to ply an interactive poker website in Nevada. MGM Resorts officials told regulators the assembly would pause until real-money poker and casino online troop Bwin.Party Digital Pleasure plc is licensed by the nation before launching its website. Bwin.party, MGM Resorts and Boyd Gaming Corporation receive a partnership agreement to run the online poker in Nevada and in every nook the Collaborative States if Congress legalizes the activity. Bwin.Party is licensed in Gibraltar and operates all the way through Europe. The company is awaiting licensing from Nevada gaming regulators, but the germaneness isn't expected to be heard until sooner next year. Under the agreement, Bwin.Party owns 65 percent of the fbi, MGM Resorts owns 25 percent and Boyd Gaming has 10 percent.

    Reply
  • MCS

    Posted by sanjiv on 10/11/2012 08:21am

    Excellent Article. I was looking for reasons to use managed heap vs system heap for GC and found little information on the internet besides this one. Thanks.

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds