15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

312 ❘ ChaPTer 13 memOry mAnAGement And pOinters<br />

Generally, the garbage collector runs when the .<strong>NET</strong> runtime determines that garbage<br />

collection is required. You can force the garbage collector to run at a certain point in<br />

your code by calling System.GC.Collect(). The System.GC class is a .<strong>NET</strong> class<br />

that represents the garbage collector, <strong>and</strong> the Collect() method initiates a garbage<br />

collection. The GC class is intended for rare situations in which you know that it’s a<br />

good time to call the garbage collector; for example, if you have just de-referenced<br />

a large number of objects in your code. However, the logic of the garbage collector<br />

does not guarantee that all unreferenced objects will be removed from the heap in a<br />

single garbage collection pass.<br />

When the garbage collector runs, it actually hurts the performance of your application as it is impossible for<br />

your application to continue running while the garbage collector fi nishes its tasks. Use the .<strong>NET</strong> garbage<br />

collector to make this less of a problem, as it is a generational garbage collector .<br />

When objects are created, they are placed within the managed heap. The fi rst section of the heap is called<br />

the generation 0 section or gen 0. As your new objects are created, they are moved into this section of the<br />

heap. Therefore, this is where the youngest objects reside.<br />

Your objects are there until the fi rst collection of objects occurs through the garbage collection process.<br />

The objects that remain alive after this cleansing are compacted <strong>and</strong> then moved to the next section or<br />

generational part of the heap — the generation 1 or gen 1 section.<br />

At this point, the generation 0 section is empty, <strong>and</strong> all new objects are again placed in this section.<br />

Older objects that survived the GC (garbage collection) process are found further down in the generation<br />

1 section. This movement of aged items actually occurs one more time. The next collection process that<br />

occurs is then repeated. This means that the items that survived the GC process from the generation<br />

1 section are moved to the generation 2 section, <strong>and</strong> the gen 0 items go to gen 1, again leaving gen 0 open<br />

for new objects.<br />

It is interesting to note that a garbage collection will occur when you allocate an item<br />

that exceeds the capacity of the generation 0 section or when a GC.Collect() is called.<br />

This process greatly improves the performance of your application. It is generally observed that your<br />

youngest objects are usually the ones that can be collected <strong>and</strong> that there are a large number of younger -<br />

related objects that might be reclaimed as well. If these objects reside next to each other in the heap, then<br />

the garbage collection process will be faster. In addition, the fact that related objects are residing next to<br />

each other will also make program execution faster all around.<br />

Another area in the performance realm of garbage collection in .<strong>NET</strong> is in how the framework deals with<br />

larger objects that go onto the heap. Under the covers of .<strong>NET</strong>, larger objects have their own managed heap,<br />

referred to as the Large Object Heap. When objects greater than 85,000 bytes are utilized, they go to this<br />

special heap rather than the main heap. Your .<strong>NET</strong> application doesn ’ t know the difference, as this is all<br />

managed for you. The reason for this is that compressing large items in the heap is expensive <strong>and</strong> therefore<br />

isn ’ t done for the objects residing in the Large Object Heap.<br />

freeing unmanaged resourCes<br />

The presence of the garbage collector means that you will usually not worry about objects that you no<br />

longer need; you will simply allow all references to those objects to go out of scope <strong>and</strong> allow the garbage<br />

collector to free memory as required. However, the garbage collector does not know how to free unmanaged<br />

resources (such as fi le h<strong>and</strong>les, network connections, <strong>and</strong> database connections). When managed classes<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!