What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleForce garbage collection in Visual Basic .NET
DescriptionThis example shows how to force garbage collection in Visual Basic .NET.
Keywordsgarbage collection, garbage collector, GC, VB.NET
CategoriesVB.NET, Software Engineering


When a program creates an object, the object takes up memory. Later, when the program has no more references to the object, the object's memory becomes unreachable but it is not immediately freed. Note also that the object's Finalize event is not called at this point.

After a lot of memory has been "lost" to the program in this way, the Garbage Collector may decide it's time to clean house. First it marks all of the memory that has been used by objects as not in use. It then follows all of the references that are accessible to the program and marks the objects they refer to as in use. Finally, the Garbage Collector frees any objects that are still not marked as in use. At this point, the Finalize methods for those objects execute.

Normally you should leave the Garbage Collector alone. It usually does a decent job of deciding when cleaning house will be worthwhile.

However, you may sometimes know something about the way the program works that lets you know it would be helpful to perform garbage collection. For example, suppose the program creates a huge number of objects, uses them for a while, and then releases them. At that point, it might be worth reclaiming the objects' memory so tou might force garbage collection. (Note: If you don't force garbage collection, it will still occur later when the program needs more memory.)


When you click this program's Allocate Object button, the program creates a new object. The button's Click event handler does not save a reference to the object so it becomes eligible for garbage collection as soon as the event handler exits.

' Create an object.
Private Sub btnAllocateObject_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    Dim obj As New TestClass
    obj.ObjectNumber = m_ObjectNumber
    MessageBox.Show("Created object " & m_ObjectNumber)
    m_ObjectNumber += 1
End Sub
When you click the Force Garbage Collection button, the program calls GC.Collect to force garbage collection. (See the online help for information on garbage collection generations.
' Collect all garbage generations.
Private Sub btnForceGarbageCollection_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
End Sub
The test class displays a message box when its Finalize method executes. When you run the program, you will not see this message until garbage collection.
Public Class TestClass
    Public ObjectNumber As Integer

    ' Display a finalization message.
    Protected Overrides Sub Finalize()
        MessageBox.Show("Finalizing " & ObjectNumber)
    End Sub
End Class
Garbage collection also occurs when the program ends. To see this, create a few objects and then close the program without forcing garbage collection first.

My book Expert One-on-One Visual Basic 2005 Design and Development explains garbage collection in more detail.

Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.