Resource Management and IDisposable

I l @ ve RuBoard

Resource Management and IDisposable

Despite what some might think, resource management is still an important part of developing applications with Visual Basic .NET. You might assume that the garbage collector is supposed to do all of your cleanup and memory management for you, and for the most part that's true. The problem is that applications can have specific resource requirements that the garbage collector cannot anticipate. So even though the garbage collector can deal with most of the memory management aspects of your applications, in some situations you need to do some additional work.

The reason this can become an issue is the nondeterministic nature of garbage collection. There are no guarantees about when objects will be cleaned up ”only that they'll be cleaned up eventually. This is a problem mainly when you mix very strict performance and scalability requirements with objects that allocate unmanaged system resources (file handles, GDI+ graphics devices, window handles, COM objects, and so forth). If you have an application or service that has to do a lot of work or deals with many simultaneous incoming requests , you might run out of system resources quickly if you don't dispose of them quickly. In other words, your application might get ahead of the garbage collection cleanup and exceed the available resource on the system before the garbage collector gets around to cleaning up the unused objects.

Obviously, this can be a real problem ”not just the cleaning up, but identifying which objects need to be cleaned up immediately. The CLR solution to this problem is the IDisposable interface. IDisposable defines only one method: IDisposable.Dispose . The idea behind IDisposable is simple. If your object allocates unmanaged resources that persist for the lifetime of the object, you should implement IDisposable . This rule applies to your own code as well to the classes in the .NET Framework.

Note

Nothing in Visual Basic .NET or the CLR forces you to clean up unmanaged resources in your own classes or to implement IDisposable . This is simply the recommended practice, and it ensures that other developers will know which classes require additional cleanup.


I l @ ve RuBoard


Designing Enterprise Applications with Microsoft Visual Basic .NET
Designing Enterprise Applications with Microsoft Visual Basic .NET (Pro-Developer)
ISBN: 073561721X
EAN: 2147483647
Year: 2002
Pages: 103

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net