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 (Add Memory Pressure – Remove Memory Pressure) using System.Gc class - VB.net - C# .net.Part1

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

In this simple article you will learn that how garbage collector works, how we can add and remove memory pressures using System.Gc class in VB and C# .net.

 Garbage Collector:

This is service that automatically reclaims unused memory. The memory that will not used by program in future is collected by garbage collector so that other programs can use this memory.  A garbage collector is rum after an unspecified time or when runtime thinks that it is necessary to run.

In dot net we can control some feature of garbage collector by using system.Gc class. Two major functions of System.Gc are Add Memory Pressure and Remove Memory Pressure. A managed object automatically calls Add Memory Pressure function after allocating the unmanaged memory in constructor and Remove Memory Pressure function after releasing this unmanaged memory in finalize.

We can call these functions by using System.Gc class. All the functions in System.Gc class are static so they can be called by class name.

Add Memory Pressure:

To determine that when the garbage collector will run to collect the unused memory, runtime checks that how much managed memory is allocated for a program. If the small memory managed object allocates a large unmanaged memory then runtime does pay attention that this is urgent to run garbage collector because it sees that there is small memory for managed object. We can use Add Memory Pressure function to inform the run time about this memory pressure.

When the unmanaged memory allocation is take place during the life time of the managed object we can inform run time about the unmanaged memory pressure on system memory by using Add Memory Pressure function. This function notifies the run time about memory pressure. This function takes a long type number which tells the number of bytes allocated and returns nothing.

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_makememory_Click(object sender, EventArgs e)

        {

            try

            {

                if (txt_add.Text != "")

                {

                    long bytes = (long)Convert.ToInt64(txt_add.Text);

                    System.GC.AddMemoryPressure(bytes);

                    MessageBox.Show("MEMORY PRESSURE ADDED");

                }

                else

                    MessageBox.Show("PLEASE ENTER NUMBER OF BYTES");

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.StackTrace);

            }

          }

VB

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

        Try

            If txt_add.Text <> "" Then

                Dim bytes As Long = CLng(Convert.ToInt64(txt_add.Text))

                System.GC.AddMemoryPressure(bytes)

                MessageBox.Show("MEMORY PRESSURE ADDED")

            Else

                MessageBox.Show("PLEASE ENTER NUMBER OF BYTES")

            End If

        Catch ex As Exception

            MessageBox.Show(ex.StackTrace)

        End Try

    End Sub

This is simple code to notify run time about memory pressure.

Remove Memory Pressure:

After adding the memory pressure if the unmanaged memory is released then it is necessary to notify run time that memory is released and run time is now no longer needs to take that memory into account when scheduling garbage collection. For this purpose we can use Remove Memory Function of the System.Gc. This function notifies the run time that memory pressure has been released. This function takes a long type number which tells the number of bytes released and returns nothing.

Note that number of bytes in add memory pressure function and remove memory pressure function should be same other wise it can effect the performance of system in case where applications run for a long period.

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_removememory_Click(object sender, EventArgs e)

        {

            try

            {

                if (txt_remove.Text != "")

                {

                    long bytes = (long)Convert.ToInt64(txt_remove.Text);

                    System.GC.RemoveMemoryPressure(bytes);

                    MessageBox.Show("MEMORY PRESSURE REMOVED");

                }

                else

                    MessageBox.Show("PLEASE ENTER NUMBER OF BYTES");

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.StackTrace);

            }

        }

VB

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

        Try

            If txt_remove.Text <> "" Then

                Dim bytes As Long = CLng(Convert.ToInt64(txt_remove.Text))

                System.GC.RemoveMemoryPressure(bytes)

                MessageBox.Show("MEMORY PRESSURE REMOVED")

            Else

                MessageBox.Show("PLEASE ENTER NUMBER OF BYTES")

            End If

        Catch ex As Exception

            MessageBox.Show(ex.StackTrace)

        End Try

    End Sub

This is simple code to notify runtime that memory pressure has been released.

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 garbage collector works, how we can add and remove memory pressures using System.Gc class in VB and C# .net

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Convert byte array to char – double – int and single using BitConverter class - VB.net - C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net