Lesson 6: Garbage Collection

Lesson 6: Garbage Collection

The automatic memory management scheme employed by the .NET Framework is called garbage collection. Memory from objects that are no longer used is traced and reclaimed without any action required by the application. In this lesson, you learn how garbage collection works.

After this lesson, you will be able to

  • Describe how garbage collection manages the reclamation of unused memory

  • Describe how garbage collection deals with circular references

Estimated lesson time: 15 minutes

The .NET Framework employs automatic memory management, which means that when an object is no longer being used, the .NET Framework automatically reclaims the memory that was being used by that object. This process is called garbage collection. Consider the following example:

Visual Basic .NET

Sub GarbageCollectionExample1() Dim myWidget As New Widget() End Sub

Visual C#

void GarbageCollectionExample1() { Widget myWidget = new Widget(); }

When this procedure ends, the variable myWidget goes out of scope and the object it refers to is no longer referenced by any application variable. The garbage collector continuously traces the reference tree in the background and identifies objects that no longer have references. When it finds one, such as the Widget in the previous example, it deletes it and reclaims the memory. Because the garbage collector is always running, you do not have to explicitly destroy objects when you are finished with them.

The garbage collector is a low-priority thread under normal circumstances. It operates when processor time is not consumed by more important tasks. When memory becomes limited, however, the garbage collector thread moves up in priority. Memory is reclaimed at a more rapid pace until it is no longer limited, at which point the priority of garbage collection is again lowered.

This non-deterministic approach to memory reclamation seeks to maximize application performance and supplies a less bug-prone application environment. There is a cost, however. Because of the mechanism by which garbage collection operates, you cannot be certain when an object will be reclaimed. Thus, you have no control over when a class s destructor (Visual C#) or finalizer (Visual Basic .NET) is executed. These methods should not contain code that you rely on being run at a given time. Instead, classes that appropriate expensive resources usually implement a Dispose() method to explicitly free those resources when the class is no longer needed.

Circular References

Garbage collection also manages circular references, previously a common form of memory leak. Consider the following example:

Visual Basic .NET

Class Widget Public ChildWidget As Widget Public Parent As Widget End Class Class aClass Public GrandParent As Widget Sub Demo() Dim Parent As Widget Dim Child As Widget GrandParent = New Widget() GrandParent.ChildWidget = New Widget() Parent = GrandParent.ChildWidget Parent.ChildWidget = New Widget() Child = Parent.ChildWidget Child.Parent = Parent GrandParent = Nothing End Sub End Class

Visual C#

class Widget { public Widget ChildWidget; public Widget Parent; } class aClass { Widget GrandParent; void Demo() { Widget Parent; Widget Child; GrandParent = new Widget(); GrandParent.ChildWidget = new Widget(); Parent = GrandParent.ChildWidget; Parent.ChildWidget = new Widget(); Child = Parent.ChildWidget; Child.Parent = Parent; GrandParent = null; } }

The Widget class consists of two fields: a ChildWidget field that holds a reference to a Widget object and a Parent field that holds a reference to another Widget object. In this example, a Widget object is created and assigned to the variable GrandParent. This object then spawns another Widget object and assigns it to its ChildWidget field. The Parent variable is also assigned to point to this object. Parent, in turn, creates a third Widget, which is assigned to both the ChildWidget field of Parent and to the Child variable. The Parent field of the Child variable is assigned to Parent, thus creating a reference from Child to Parent. When the GrandParent variable is set to nothing, the Widget objects represented by Parent and Child are left referring only to each other a circular reference.

Although circular references can create difficult-to-locate memory leaks in other development platforms, the .NET Framework garbage collector is able to trace and remove such memory leaks. Thus, if a pair of objects are only referenced by each other, they will be marked for garbage collection.

Lesson Summary

  • The .NET Framework provides automatic memory reclamation through the garbage collector. The garbage collector is a low-priority thread that always runs in the background of the application. When memory is scarce, the priority of the garbage collector is elevated until sufficient resources are reclaimed.

  • Because you cannot be certain when an object will be garbage collected, you should not rely on code in finalizers or destructors being run within any given time frame. If you have resources that need to be reclaimed as quickly as possible, provide a Dispose() method that gets called explicitly.

  • The garbage collector continuously traces the reference tree and disposes of objects containing circular references to one another in addition to disposing of unreferenced objects.



MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[.  .. ]0-316
MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[. .. ]0-316
ISBN: 735619263
EAN: N/A
Year: 2003
Pages: 110

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