Demonstrates forced garbage collection
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// gc.cs -- Demonstrates forced garbage collection
//
// Compile this program with the following command line:
// C:>csc gc.cs
//
namespace nsGarbage
{
using System;
using System.Threading;
public class GCDemo
{
static public void Main ()
{
long Mem = GC.GetTotalMemory (false);
Console.WriteLine ("Beginning allocated memory is " + Mem);
for (int x = 0; x < 10000; ++x)
{
clsClass howdy = new clsClass();
}
Mem = GC.GetTotalMemory (false);
Console.WriteLine ("Allocated memory before garbage collection is " + Mem);
GC.Collect ();
Mem = GC.GetTotalMemory (true);
Console.WriteLine ("Allocated memory after garbage collection is " + Mem);
}
}
class clsClass
{
public clsClass () { }
public int x = 42;
public float f = 2E10f;
public double d = 3.14159;
public string str = "This here's a string";
}
}
Related examples in the same category