Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Garbage Collector - memory operations (Get Collector count- Get - Maximum Generation) using System.Gc class - VB.net - C# .net.Part2

Author: Shehzad Hemani
Download Source Code : 1012_source.zip

In this simple article you will learn that how we can get maximum generation supported by system, generation of a specific object and how many times collector has run for specific generation objects.

 Maximum Generation:

Object aging allows garbage collector to collect memory of objects of specific generation so collector does not have to evaluate all objects for all generations. If you want to get the maximum generation number which is supported by your system for this purpose Gc class provides us a property named MaxGeneration. This property returns an integer as generation number supported by the system.

To demonstrate make a window application. Drag one button on form.

Now write the following code on Button click event:

C#

private void btn_max_Click(object sender, EventArgs e)

        {

            int max = System.GC.MaxGeneration;

            string str = max.ToString();

            MessageBox.Show("MAXIMUM GENERATION NUMBER IS  :" + str);

        }

VB

Private Sub btn_max_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim max As Integer = System.GC.MaxGeneration

        Dim str As String = max.ToString()

        MessageBox.Show("MAXIMUM GENERATION NUMBER IS :" & str)

    End Sub

This is simple code to get maximum number of generation supported by system.

Get Generation:

If you want to get the generation of a specific object for this purpose Gc class provides us a function named GetGeneration (). This function takes object as parameter and returns an integer number as generation of that object.

To demonstrate make a window application. Drag one button on form.

Now write the following code on Button click event:

C#

private void btn_getgeneration_Click(object sender, EventArgs e)

        {

            String obj = "devasp";

            int gen=100;

            gen = System.GC.GetGeneration(obj);

            string str = gen.ToString();

            MessageBox.Show("GENERATION : " +str);

        }

VB

Private Sub btn_getgeneration_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim obj As [String] = "devasp"

        Dim gen As Integer = 100

        gen = System.GC.GetGeneration(obj)

        Dim str As String = gen.ToString()

        MessageBox.Show("GENERATION : " & str)

    End Sub

This is simple code to get generation of a specific object.

Collector Count:

If you want to count that how many times garbage collector has run for a specific generation objects, for this purpose Gc class provides us a function named collectioncount (). This function takes an integer as parameter that tells the generation and returns an integer which tells that how many times collector has run for specified generation objects.

To demonstrate make a window application. Drag text box and one button on form.

Now write the following code on Button click event:

C#

private void btn_count_Click(object sender, EventArgs e)

        {

            int result = -1;

            if (txt_gen.Text != "")

            {

                int cur = System.GC.MaxGeneration;

                int val = Convert.ToInt16(txt_gen.Text);

                if (val >= 0 && val <= cur)

                {

                    result = System.GC.CollectionCount(val);

                    MessageBox.Show("COLLECTOR count : " + result.ToString());

                }

                else

                    MessageBox.Show("GENERATION IS NOT SUPPORTED BY SYSTEM");

            }

            else

            MessageBox.Show("PLEASE ENTER GENERATION");

        }

VB

Private Sub btn_count_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim result As Integer = -1

        If txt_gen.Text <> "" Then

            Dim cur As Integer = System.GC.MaxGeneration

            Dim val As Integer = Convert.ToInt16(txt_gen.Text)

            If val >= 0 AndAlso val <= cur Then

                result = System.GC.CollectionCount(val)

                MessageBox.Show("COLLECTOR count : " & result.ToString())

            Else

                MessageBox.Show("GENERATION IS NOT SUPPORTED BY SYSTEM")

            End If

        Else

            MessageBox.Show("PLEASE ENTER GENERATION")

        End If

    End Sub

This is simple code to check that how many times collector has run for a specific generation objects.

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 get maximum generation supported by system, generation of a specific object and how many times collector has run for specific generation objects using System.Gc class.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Garbage Collector - memory operations (Add Memory Pressure – Remove Memory Pressure) using System.Gc class - VB.net - C# .net.Part1

Disclaimer - Privacy
© 2002-2017 DevASP.net