using System; using System.Collections.Generic; using System.Text; public class MyResourceWrapper : IDisposable { public void Dispose() { Console.WriteLine("In Dispose() method!"); } } class Program { static void Main(string[] args) { MyResourceWrapper rw = new MyResourceWrapper(); if (rw is IDisposable) rw.Dispose(); using (MyResourceWrapper rw2 = new MyResourceWrapper()) { } } }
Development Class
Print out how many times a generation has been swept.
using System;
using System.Collections.Generic;
using System.Text;
public class Car {
private int currSp;
private string petName;
public Car() { }
public Car(string name, int speed) {
petName = name;
currSp = speed;
}
public override string ToString() {
return string.Format(“{0} is going {1} MPH”,petName, currSp);
}
}
class Program {
static void Main(string[] args) {
Car refToMyCar = new Car(“A”, 100);
Console.WriteLine(refToMyCar.ToString());
Console.WriteLine(GC.GetGeneration(refToMyCar));
object[] tonsOfObjects = new object[50000];
for (int i = 0; i < 50000; i++)
tonsOfObjects[i] = new object();
GC.Collect(0);
GC.WaitForPendingFinalizers();
Console.WriteLine("Generation of refToMyCar is: {0}",GC.GetGeneration(refToMyCar));
if (tonsOfObjects[9000] != null) {
Console.WriteLine("Generation of tonsOfObjects[9000] is: {0}",GC.GetGeneration(tonsOfObjects[9000]));
} else
Console.WriteLine("tonsOfObjects[9000] is no longer alive.");
Console.WriteLine("
Gen 0 has been swept {0} times", GC.CollectionCount(0));
Console.WriteLine("Gen 1 has been swept {0} times", GC.CollectionCount(1));
Console.WriteLine("Gen 2 has been swept {0} times", GC.CollectionCount(2));
}
public static void MakeACar() {
Car myCar = new Car();
}
}
[/csharp]
IDisposable interface
using System; namespace Client.Chapter_5___Building_Your_Own_Classes { public class DTOR: IDisposable { public static int[] MyIntArray; private static int ObjectCount = 0; private bool Disposed = false; static void Main(string[] args) { MyIntArray = new int[10]; ObjectCount++; } //Used to clean up and free unmanaged resources //Never mark this class as virtual as you do not want derived //classes to be able to override it. public void Dispose() { //if this class is derived then call the base //class dispose. //base.Dispose(); //Call the overloaded version of dispose Dispose(true); //Tell the CLR not to run the finalizer this way //you do not free unmanaged resources twice GC.SuppressFinalize(this); } //If user calls dispose both managed and unmanaged resources //are freed //If the finalizer is called then only unmanaged resources are freed private void Dispose(bool disposing) { if(!this.Disposed) { if(disposing) { //free any managed resources } //free unmanaged resources } Disposed = true; } //This finalizer method is called by the GC, //not the user. The net result of having this is that //the object will always survive the first GC cycle and //will be collected the next time GC1 is collected. ~DTOR() { Dispose(false); } } }
Get Total Memory
using System; public class MyClass { public void Dispose() { Console.WriteLine("Dispose()"); GC.SuppressFinalize(this); } } public class ReRegFinalApp { public static void DoSomething() { MyClass t = new MyClass(); Console.WriteLine(t); t.Dispose(); GC.ReRegisterForFinalize(t); } public static void Main(string[] args) { long n = GC.GetTotalMemory(true); Console.WriteLine("start of Main: {0} bytes allocated", n); DoSomething(); n = GC.GetTotalMemory(true); Console.WriteLine("end of Main: {0} bytes allocated", n); } }
MaxGeneration is zero based.
using System; using System.Collections.Generic; using System.Text; public class Car { private int currSp; private string petName; public Car() { } public Car(string name, int speed) { petName = name; currSp = speed; } public override string ToString() { return string.Format("{0} is going {1} MPH", petName, currSp); } } class Program { static void Main(string[] args) { Console.WriteLine("Estimated bytes on heap: {0}", GC.GetTotalMemory(false)); Console.WriteLine("This OS has {0} object generations. ", (GC.MaxGeneration + 1)); Car refToMyCar = new Car("Zippy", 100); Console.WriteLine(refToMyCar.ToString()); Console.WriteLine(" Generation of refToMyCar is: {0}", GC.GetGeneration(refToMyCar)); } public static void MakeACar() { Car myCar = new Car(); } }
Estimated bytes on heap
using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { Console.WriteLine("Estimated bytes on heap: {0}", GC.GetTotalMemory(false)); Console.WriteLine("This OS has {0} object generations. ", (GC.MaxGeneration + 1)); } }
Check the Generation for an object array
using System;
using System.Collections.Generic;
using System.Text;
public class Car {
private int currSp;
private string petName;
public Car() { }
public Car(string name, int speed) {
petName = name;
currSp = speed;
}
public override string ToString() {
return string.Format(“{0} is going {1} MPH”,
petName, currSp);
}
}
class Program {
static void Main(string[] args) {
Car refToMyCar = new Car(“Zippy”, 100);
Console.WriteLine(refToMyCar.ToString());
Console.WriteLine(GC.GetGeneration(refToMyCar));
object[] tonsOfObjects = new object[50000];
for (int i = 0; i < 50000; i++)
tonsOfObjects[i] = new object();
GC.Collect(0);
GC.WaitForPendingFinalizers();
Console.WriteLine(GC.GetGeneration(refToMyCar));
}
public static void MakeACar() {
Car myCar = new Car();
}
}
[/csharp]