This blog is meant to provide information, thoughts and links that may be found useful to a computer programmer. There is no set strategy or limits of what topics will be discussed.

April 10, 2007

A brief look at Garbage Collection

Garbage Collection

What is it?

Garbage Collection (GC) is a built-in feature of the .NET Common Language Runtime (CLR). The purpose of Garbage Collection is to free managed memory resources. The reason this has come into existence is that in prior windows versions the programmer was responsible for releasing allocated memory resources when appropriate. Resources are most typically used from the creation of objects ( i.e. using the keyword "new" operator). The problem with this was that most developers (especially the hobbyist) would not properly clean applications (and their objects) from the resources; another issue had been that if an application had failed and pre-maturely terminated then the resources would remain in use until the memory was specifically told to clear the resources. The developer of complicated applications that would use a vast amount of resources had to create methods to determine if a failure had occurred and have the memory cleared prior to restarting the memory intensive application.

 

Microsoft's answer was to create the Garbage Collection class. This class would periodically scan the current allocated memory resources and determine if any resources were in use by programs that have already been terminated (rather it be intentional or accidental). Should the Garbage Collection find a used resource with an invalid application reference it would then clear the resource to free up what could be a valuable resource.

How do you use it?

Microsoft recommends that you do not use the Garbage Collector class directly; however, they have left the class open to be used in the few and rare instances it may need to be called. The reason Microsoft recommends against its use is that the Garbage Collector is an automated task performed by Microsoft Windows through the .NET Framework ( 1.1 and higher). The primary reason not to use the Garbage Collector class is that each time the GC is called it will suspend all active threads. This can cause serious degradation in performance should the GC class be called too often.

 

Since this is an automated feature you may ask why you would even bother to learn about this feature. The primary reason is that since you won't control when this feature is initiated you could inadvertently lock your application (or absorb large performance degradation). A prime example would be an application that supports multiple data connections through an SQL adapter. The default SQL adapter uses a pool of 10 connections. It is conceivable if you were to use all 10 connections in your application and not properly release the connections from memory, and then call another connection into memory that you would have to wait until the Garbage Collection had occurred to clear the memory of these connections, even though you have already closed the prior 10 connections before calling the additional connections!

 

So, when is an instance you would want to call the GC class, and how would you call it? One instance would be the above example of utilizing a large pool of SQL connections and releasing them all at one moment. Another time you would want to call it is if you have an application that uses a document that contains a large number of references using unmanaged memory resources and your application closes this document; you then would know that all of the resources should be released since the document no longer exists (suspension of threads referencing the document would not effect your application; however, still use caution in case of other threads are still in use in your application). You would then want to call the GC.Collect method. Again, you will want to ensure this is placed in a block of coding that is limited in calls, preferably called only once. A great example would be to place any method calls to GC.Collect in the form's Finalize class-method; you would "override" the finalize method to include the GC.Collect class and proper calls.

 

What can I do to release memory resources, without calling GC.Collect?

It is best described in the MSDN webpage titled "Garbage Collection". I will use a single method to describe how to release a memory resource; however, keep in mind that there are a few different ways and many of them shall depend on the chosen programming language. Such as C# uses additional features for releasing memory that cannot be used in Visual Basic. You can read more detail at: http://msdn2.microsoft.com/en-us/library/fs2xkftw(VS.80).aspx .

 

You can simply call the Dispose() method directly from your application. An example in Visual Basic would be:

Public Sub Close()

' Calls the Dispose method without parameters.

Dispose()

End Sub

 

An example in C# would be:

public void Close()

{

// Calls the Dispose method without parameters.

Dispose();

}

 

These two methods show a very simplistic method of disposing of your object; however, you will want to review the above mentioned website for the preferred patterns and practices that will ensure proper disposing of the object by implementing the IDisposable class.

 

This should get you started and should help you to understand how to make your programs less susceptible to degradation from memory resources not being properly released.

No comments: