[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Force garbage collection in C#

[Force garbage collection in C#]

This example shows how you can force garbage collection in C#. The .NET languages use a garbage collection scheme of memory management.

There are a few weird details, but the basic process is this. When the application is running low on memory, the garbage collector can decide to run. When it does, it marks all of the memory the program has used as not-in-use. It then scans all of the objects that are accessible by the program and marks them as in-use. Finally it moves the objects that are still marked not-in-use into the pool of free memory.

The process of freeing an object's memory is called finalization. Because you don't know when finalization may occur, the whole process is called non-deterministic finalization.

Normally you shouldn't mess with the garbage collector because it does a pretty good job on its own. If you force garbage collection to occur, you'll probably make the program run less efficiently. However, if you're writing destructors for objects, you may want to force garbage collection to test them.

The following code shows how this example forces garbage collection when you click the Collect button.

// Force garbage collection. private void btnCollect_Click(object sender, EventArgs e) { GC.Collect(); }

That's all there is to it. (You can also pass the Collect method a parameter to tell it what generation to collect, but that requires you to understand a whole new level of garbage collection detail that you really don't need to know about most of the time.)

The following code shows the Person class.

class Person { public Person() { Console.WriteLine("Person:Create"); } ~Person() { Console.WriteLine("Person:Finalize"); } }

This class displays messages in the Output window showing when its objects are created and finalized.

Run the program and click the Create Person button a few times to create Person objects. The output window will show object creation. The following code shows how the program creates the Person objects.

// Create a Person. private void btnCreatePerson_Click(object sender, EventArgs e) { Person person = new Person(); }

This code simply creates a Person object and then exits. After the method finishes, the program doesn't have any reference to the new object so its memory is effectively lost to the program. The next time the garbage collector runs, it will reclaim the object.

Unless you click the Create Person button a whole bunch of time (thousands), the program probably won't run low on memory so the garbage collector won't run.

If you click the Collect button, the garbage collector runs and you should see Finalize messages in the Output window for each of the Person objects you created.

Create some more Person objects and close the program. The garbage collector runs again and you should see Finalize messages for any objects you created after clicking the Collect button.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.