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 Total Memory- Stop and Resume finalizer) using System.Gc class - VB.net - C# .net.Part3

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

In this simple article you will learn that how we can get total memory allocated currently, stop and resume finalizer for an object using System.Gc class.

 Total Memory:

If you want to get the total memory currently allocated for this purpose Gc class provides us a function named GetTotalMemory (). This function takes an Boolean value which decides that function should wait for a little period before returning so that garbage collector can run and collect unused memory or not. If the value is true then function will wait until garbage collector runs else it will return.

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

Now write the following code on Button click event:

C#

private void btn_totalmem_Click(object sender, EventArgs e)

        {

            try

            {

                long total;

                total = System.GC.GetTotalMemory(false);

                string str = total.ToString();

                MessageBox.Show("Total Memory : " + str);

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.StackTrace);

            }

        }

VB

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

        Try

            Dim total As Long

            total = System.GC.GetTotalMemory(False)

            Dim str As String = total.ToString()

            MessageBox.Show("Total Memory : " & str)

        Catch ex As Exception

            MessageBox.Show(ex.StackTrace)

        End Try

    End Sub

This is simple code to get total memory currently allocated.

Stop Finalizer:

If you want to stop the finalizer or want finalizer not to run for a specific object for this purpose Gc class provides us a function named SuppressFinalize (). This function takes object as parameter and returns nothing. This function requests the system not to call the finalizer for the object that is passed as parameter.

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

Now write the following code on Button click event:

C#

private void btn_stop_Click(object sender, EventArgs e)

        {

            try

            {

                System.GC.SuppressFinalize(obj);

                MessageBox.Show("Finalizer stoped for specified object");

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.StackTrace);

            }

        }

VB

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

        Try

            System.GC.SuppressFinalize(obj)

            MessageBox.Show("Finalizer stoped for specified object")

        Catch ex As Exception

            MessageBox.Show(ex.StackTrace)

        End Try

    End Sub

This is simple code to stop finalizer from running for a specific object.

Resume Finalizer:

If you want to resume the finalizer for an object for which you stopped the finalizer for this purpose Gc class provides us a function named ReRegisterForFinalize (). This function takes object as parameter and returns nothing. This function requests the system to call the finalizer for the object that is passed as parameter or for which finalizer has stopped from running.

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

Now write the following code on Button click event:

C#

private void btn_resume_Click(object sender, EventArgs e)

        {

            try

            {

                System.GC.ReRegisterForFinalize(obj);

                MessageBox.Show("Finalizer Resumed for specified object");

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.StackTrace);

            }

        }

VB

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

        Try

            System.GC.ReRegisterForFinalize(obj)

            MessageBox.Show("Finalizer Resumed for specified object")

        Catch ex As Exception

            MessageBox.Show(ex.StackTrace)

        End Try

    End Sub

This is simple code to call the finalizer for the object for which the finalizer has been stopped form running.

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 total memory allocated currently, stop and resume finalizer for an object using System.Gc class.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Garbage Collector - memory operations (Get Collector count- Get - Maximum Generation) using System.Gc class - VB.net - C# .net.Part2

Disclaimer - Privacy
© 2002-2017 DevASP.net