Author: Shehzad Hemani
Download Source Code : 1014_source.zip
In this simple article you will learn that how we can run garbage collector using System.Gc class.
Run Garbage Collector:
Garbage collector runs after an unspecified time interval decided by runtime. But if you want to run the garbage collector by your self for this purpose Gc class provides us a function named collect (). This function has total 3 overloaded methods.
First Method:
This function runs garbage collector immediately or forcefully for all objects of all generations. This function takes and returns nothing but collects the unused memory of objects of all generations.
To demonstrate make a window application. Drag one button on form.
Now write the following code on Button click event:
C#
private void btn_Allgenerations_Click(object sender, EventArgs e)
{
try
{
System.GC.Collect();
MessageBox.Show("GARBAGE COLLECTOR RAN FOR ALL GENERATIONS");
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace);
}
}
VB
Private Sub btn_Allgenerations_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
System.GC.Collect()
MessageBox.Show("GARBAGE COLLECTOR RAN FOR ALL GENERATIONS")
Catch ex As Exception
MessageBox.Show(ex.StackTrace)
End Try
End Sub
This is simple code to run garbage collector forcefully for all generation objects.
Second Method:
This function runs garbage collector immediately or forcefully for all objects of given generation to the maximum generation supported by the system. This function takes an integer that specified the oldest generation on which garbage collector can perform collection and returns nothing and collects the unused memory of objects from specified generation to maximum generation supported by system.
To demonstrate make a window application. Drag one button and text box on form.
Now write the following code on Button click event:
C#
private void btn_GenerationCollector_Click(object sender, EventArgs e)
{
try
{
if (txt_gen.Text != "")
{
int gen = Convert.ToInt16(txt_gen.Text);
System.GC.Collect(gen);
MessageBox.Show("GARBAGE COLLECTOR RAN");
}
else
MessageBox.Show("PLEASE ENTER GENERATION");
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace);
}
}
VB
Private Sub btn_GenerationCollector_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
If txt_gen.Text <> "" Then
Dim gen As Integer = Convert.ToInt16(txt_gen.Text)
System.GC.Collect(gen)
MessageBox.Show("GARBAGE COLLECTOR RAN")
Else
MessageBox.Show("PLEASE ENTER GENERATION")
End If
Catch ex As Exception
MessageBox.Show(ex.StackTrace)
End Try
End Sub
This is simple code to run garbage collector with specified oldest generation till supported generation.
Third Method:
This function runs garbage collector immediately or forcefully for all objects of given generation to the maximum generation supported by the system and with mode which is given by GCCollectionMode enumeration like default, forcefully or optimize. This function takes an integer that specified the oldest generation on which garbage collector can perform collection and a mode which is provided by GCCollectionMode enumeration and returns nothing and collects the unused memory of objects from specified generation to maximum generation supported by system with specified mode.
To demonstrate make a window application. Drag one button and text box on form.
Now write the following code on Button click event:
C#
private void btn_genwithoption_Click(object sender, EventArgs e)
{
try
{
if (txt_gen.Text != "")
{
int gen = Convert.ToInt16(txt_gen.Text);
System.GC.Collect(gen,GCCollectionMode.Optimized);
MessageBox.Show("GARBAGE COLLECTOR RAN");
}
else
MessageBox.Show("PLEASE ENTER GENERATION");
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace);
}
}
VB
Private Sub btn_genwithoption_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
If txt_gen.Text <> "" Then
Dim gen As Integer = Convert.ToInt16(txt_gen.Text)
System.GC.Collect(gen, GCCollectionMode.Optimized)
MessageBox.Show("GARBAGE COLLECTOR RAN")
Else
MessageBox.Show("PLEASE ENTER GENERATION")
End If
Catch ex As Exception
MessageBox.Show(ex.StackTrace)
End Try
End Sub
This is simple code to run garbage collector with specified mode and oldest generation till supported generation.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "DEVASP GARBAGE COLLECTOR APPLICATION";
}
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Me.Text = "DEVASP GARBAGE COLLECTOR APPLICATION "
End Sub
This simple article tells that how we can run garbage collector using System.Gc class.