hen the .NET framework instantiates an object, it allocates memory for that object on the managed heap. The object remains on the heap until it's no longer referenced by any active code, at which point the memory it's using is "garbage," ready for memory deallocation by the .NET Garbage Collector (GC). Before the GC deallocates the memory, the framework
calls the object's Finalize()
method, but developers
are responsible for calling the Dispose()
The two methods are not equivalent. Even though both methods perform object cleanup, there are distinct differences between them. To design efficient .NET applications, it's essential that you have a proper understanding of both how the .NET framework cleans up objects and when and how to use the Finalize
methods. This article discusses both methods and provides code examples and tips on how and when to use them.
To implement the techniques discussed in the article, the minimum requirements are:
An Insight into the Dispose and Finalize Methods
- .NET framework version 1.1 or higher
- Operating System: Windows XP/2000 or higher
The .NET garbage collector manages the memory of managed
objects (native .NET objects) but it does not manage, nor is it directly able to clean up unmanaged
resources. Managed resources are those that are cleaned up implicitly by the garbage collector. You do not have to write code to release such resources explicitly. In contrast, you must clean up unmanaged resources (file handles, database collections, etc.) explicitly in your code.
There are situations when you might need to allocate memory for unmanaged resources from managed code. As an example, suppose you have to open a database connection from within a class. The database connection instance is an unmanaged resource encapsulated within this class and should be released as soon as you are done with it. In such cases, you'll need to free the memory occupied by the unmanaged resources explicitly, because the GC doesn't free them implicitly.
Briefly, the GC works as shown below:
- It searches for managed objects that are referenced in managed code.
- It then attempts to finalize those objects that are not referenced in the code.
- Lastly, it frees the unreferenced objects and reclaims the memory occupied by them.
The GC maintains lists of managed objects arranged in "generations." A generation is a measure of the relative lifetime of the objects in memory. The generation number indicates to which generation an object belongs. Recently created objects are stored in lower generations compared to those created earlier in the application's life cycle. Longer-lived objects get promoted to higher generations. Because applications tend to create many short-lived objects compared to relatively few long-lived objects, the GC runs much more frequently to clean up objects in the lower generations than in the higher ones.
Keep this information about the .NET garbage collector in mind as you read the remainder of the article. I'll walk you through the Finalize
method first, and then discuss the Dispose