class Person {
byte[] bytes = new byte[50000];
public string Name { get; set; }
public Person(string Name) {
this.Name = Name;
Console.WriteLine($"Initializeing {Name}");
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("memory in use");
Console.WriteLine(GC.GetTotalMemory(false));
Console.ReadKey();
var p1 = new Person("Pawel");
Console.WriteLine(GC.GetTotalMemory(false));
Console.ReadKey();
var p2 = new Person("Marin");
Console.WriteLine(GC.GetTotalMemory(false));
Console.ReadKey();
var p3 = new Person("Magda");
Console.WriteLine(GC.GetTotalMemory(false));
Console.ReadKey();
var p4 = new Person("Tomek");
Console.WriteLine(GC.GetTotalMemory(false));
Console.ReadKey();
p1 = null;
Console.WriteLine("Dereference Pawel");
GC.Collect();
Console.WriteLine(GC.GetTotalMemory(true));
Console.ReadKey();
p2 = null;
Console.WriteLine("Dereference Marin");
GC.Collect();
Console.WriteLine(GC.GetTotalMemory(true));
Console.ReadKey();
p3 = null;
Console.WriteLine("Dereference Magda");
GC.Collect();
Console.WriteLine(GC.GetTotalMemory(true));
Console.ReadKey();
p4 = null;
Console.WriteLine("Dereference Tomek");
GC.Collect();
Console.WriteLine(GC.GetTotalMemory(true));
Console.ReadKey();
}
}
let's modify our Person class
class Person
{
public Person(string Name)
{
this.Name = Name;
Console.WriteLine($"Initializeing {Name}");
}
bool living = true;
public string Name { get; set; }
byte[] bytes = new byte[50000];
public async void BackgroundTask()
{
while (living)
{
await Task.Delay(5000);
Console.WriteLine($"{Name}" + GC.GetTotalMemory(true));
}
}
}
class Program
{
static void Main(string[] args)
{
//create pawel
var p1 = new Person("Pawel");
p1.BackgroundTask();
Console.WriteLine(GC.GetTotalMemory(true));
Console.ReadKey();
//dereference
pawel
p1 = null;
Console.WriteLine("Dereference Pawel");
GC.Collect();
Console.WriteLine(GC.GetTotalMemory(true));
Console.ReadKey();
//create marin
var p2 = new Person("Marin");
p2.BackgroundTask();
Console.WriteLine(GC.GetTotalMemory(false));
Console.ReadKey();
//dereference
marin
p2 = null;
Console.WriteLine("Dereference Marin");
GC.Collect();
Console.WriteLine(GC.GetTotalMemory(true));
Console.ReadKey();
//create magda
var p3 = new Person("Magda");
p3.BackgroundTask();
Console.WriteLine(GC.GetTotalMemory(false));
Console.ReadKey();
//derefrence
magda
p3 = null;
Console.WriteLine("Dereference Magda");
GC.Collect();
Console.WriteLine(GC.GetTotalMemory(true));
Console.ReadKey();
//create tomek
var p4 = new Person("Tomek");
p4.BackgroundTask();
Console.WriteLine(GC.GetTotalMemory(false));
Console.ReadKey();
//dereference
tomek
p4 = null;
Console.WriteLine("Dereference Tomek");
GC.Collect();
Console.WriteLine(GC.GetTotalMemory(true));
Console.ReadKey();
Console.WriteLine("done");
Console.ReadKey();
}
}
what our Person class needs is some sort of way to deactivate the background task so that the garbage collector can collect our dereferenced instances of the "Person" class. In comes the IDisposable interface, this interface forces us to create a dispose method who's job is to clean up our class for garbage collection.
class Person : IDisposable
{
public Person(string Name)
{
this.Name = Name;
Console.WriteLine($"Initializeing {Name}");
}
bool living = true;
public string Name { get; set; }
byte[] bytes = new byte[50000];
public async void BackgroundTask()
{
while (living)
{
await Task.Delay(5000);
Console.WriteLine($"{Name}" + GC.GetTotalMemory(true));
}
Console.WriteLine($"{Name} Background task complete");
}
public void
Dispose()
{
Console.WriteLine($"Disposing {Name}");
living = false;
}
}
class Program
{
static void Main(string[] args)
{
//create pawel
var p1 = new Person("Pawel");
p1.BackgroundTask();
Console.WriteLine(GC.GetTotalMemory(true));
Console.ReadKey();
//dereference
pawel
p1.Dispose();
p1 = null;
Console.WriteLine("Dereference Pawel");
GC.Collect();
Console.WriteLine(GC.GetTotalMemory(true));
Console.ReadKey();
//create marin
var p2 = new Person("Marin");
p2.BackgroundTask();
Console.WriteLine(GC.GetTotalMemory(false));
Console.ReadKey();
//dereference
marin
p2.Dispose();
p2 = null;
Console.WriteLine("Dereference Marin");
GC.Collect();
Console.WriteLine(GC.GetTotalMemory(true));
Console.ReadKey();
//create magda
var p3 = new Person("Magda");
p3.BackgroundTask();
Console.WriteLine(GC.GetTotalMemory(false));
Console.ReadKey();
//derefrence
magda
p3.Dispose();
p3 = null;
Console.WriteLine("Dereference Magda");
GC.Collect();
Console.WriteLine(GC.GetTotalMemory(true));
Console.ReadKey();
//create tomek
var p4 = new Person("Tomek");
p4.BackgroundTask();
Console.WriteLine(GC.GetTotalMemory(false));
Console.ReadKey();
//dereference
tomek
p4.Dispose();
p4 = null;
Console.WriteLine("Dereference Tomek");
GC.Collect();
Console.WriteLine(GC.GetTotalMemory(true));
Console.ReadKey();
Console.WriteLine("done");
Console.ReadKey();
Console.WriteLine(GC.GetTotalMemory(true));
Console.ReadKey();
}
}
class Program
{
static void Main(string[] args)
{
using (var p1 = new Person("Pawel"))
{
p1.BackgroundTask();
Console.WriteLine($"Memory Usage: {GC.GetTotalMemory(true)}");
Console.ReadKey();
}
Console.WriteLine("done");
Console.ReadKey();
}
}