Chapter 7. Commenting Code

Garbage Collection

In previous editions of Visual Basic, object lifetime was governed by a reference counting scheme as defined by the COM architecture. Visual Basic .NET has discarded the reference counting mechanism in favor of a garbage collector. The specific type of garbage collection implemented by .NET is called reference-tracing garbage collection. Essentially, the garbage collector monitors the resources used by a program, and when resources reach a defined threshold, the garbage collector proceeds to look for unused objects. When the garbage collector finds an object that no longer has an active reference, it destroys the object, freeing all the memory and resources the object was using. The garbage collector is smart enough to recognize circular references (where two or more objects hold a reference to one another, but none of which are held by an object still in use), and it's capable of destroying these objects. Previously, you had to resort to some memory tricks to create circular references that could be terminated easily.

There are many reasons Microsoft made the switch to garbage collection, including the claim that the overhead of reference counting made the scheme impractical in the all-object world of the .NET Framework. In addition, the allocation of new objects in memory is much faster under a garbage collection scheme because no referencing mechanism has to be initialized. While garbage collection does make life easier in some ways (such as remedying the problem of circular references), it also brings with it a new set of challenges. With COM objects, for example, an object was destroyed by the underlying architecture when its last reference was released. Setting an object variable to Nothing or simply letting it go out of scope was often sufficient enough to destroy the COM object. This isn't true in .NET.

Just because the last reference to an object is released doesn't mean the object gets destroyed at least not right away. This is because the garbage collector searches for unused objects only periodically, based on system resources and other factors. Because no reference counting is involved, the garbage collector has no idea when an object has been released; releasing the reference doesn't trigger the garbage collector to physically destroy the object. This is known as nondeterministic finalization, as opposed to the deterministic finalization found in COM.

When the garbage collector does kick in and locates unused objects, those objects are fully destroyed and any resources they were using get released. If an object that you're using holds a system resource, such as an open Winsock connection, that resource might not be released when the object is no longer used; it won't be released until the object is collected. As you can well imagine, instantiating and releasing just a few objects that use scarce resources could cause all of the available resources to be held indefinitely, even though all references to the objects using the resources have been released.

You can do a number of things to make sure your objects function correctly with respect to garbage collection. In fact, many of the directives in this chapter relate to designing and using objects efficiently under the .NET garbage collection scheme. Of these, implementing and calling a Dispose method on your objects is of particular importance. Be sure to read and understand all of the directives related to garbage collection doing so will help you create more efficient and stable code.

 

Goals of Programming Objects

The goals of programming objects are

  • Early binding objects whenever possible

  • Creating efficient code

  • Creating code that handles future enhancements with few or no complications

  • Designing objects that work well within the garbage collection scheme of the .NET Framework

  • Using .NET objects whenever possible

 



Practical Standards for Microsoft Visual Basic. NET
Practical Standards for Microsoft Visual Basic .NET (Pro-Developer)
ISBN: 0735613567
EAN: 2147483647
Year: 2005
Pages: 84

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