Destructor and Finalizer:
Destructor are called by the CLR when the Garbage Collector destroys an object. In VB.NET it is called the Finalize() method. In C# it is declared by putting a tilda '~' in front of the class name (~MyClass() {}). Destructors aren't the best place to put clean up code. This is because it is only called by the GC and you can't determine when it will be called (you can force it to be called, but that's not recommended).
Dispose:
The Dispose() method is simply a method you call manually to clean up yourobject. For this explicit clean up, .NET provides the dispose design pattern. Objects which need explicit clean up of unmanaged resources(connection object,File objects, etc..) implement the IDisposable interface. The IDisposable interface consists of the Dispose method, which unlike the Finalize method is under the control of the developer.
The recommended practice is to implement both Finalize as well as Dispose methods on an object which needs to clean up unmanaged resources. The Finalize method would serve as a backup mechanism in the event that the Dispose is never called. The garbage collector would perform the object finalization and prevent a permanent leak of the unmanaged resource.
Showing posts with label Memory Management. Show all posts
Showing posts with label Memory Management. Show all posts
Destructor and Forced Garbage Collection in .NET Framework
Destructor :
In simple terms a destructor is a member that implements the actions required to destruct an instance of a class. The destructors enable the runtime system. Destructor method is called only by .Net frameworks Garbage Collector (GC).
Forced Garbage Collection :
Foced Garbage collection is executed by means of calling System.GC.Collect() method. The garbage collector checks to see if there are any objects in the heap that are no longer being used by the application(checking the references exist for an object).
class Person
{
public Person() //Constructor
{
Console.WriteLine("Person {0} constructor", GetHashCode());
}
~Person() //Destructor
{
Console.WriteLine("Person {0} finalizer", GetHashCode());
}
}
class GCDemo
{
static void Main(string[] args)
{
Person p = new Person(); // The constructor is invoked here.
p = null;
Console.WriteLine("The object p will not be used anymore ");
Person pc = new Person(); // The constructor is invoked here.
pc = null;
Console.WriteLine("The object pc will not be used anymore ");
GC.Collect(); // Force to collect.
Console.WriteLine("Now the carbage collection is called forcefully...");
GC.WaitForPendingFinalizers(); // Wait until it finish
}
}
Output:
Person 33156464 constructor
The object p will not be used anymore
Person 15645912 constructor
The object pc will not be used anymore
Now the carbage collection is called forcefully...
Person 33156464 finalizer
Person 15645912 finalizer
Conclusion:
In simple terms a destructor is a member that implements the actions required to destruct an instance of a class. The destructors enable the runtime system. Destructor method is called only by .Net frameworks Garbage Collector (GC).
Forced Garbage Collection :
Foced Garbage collection is executed by means of calling System.GC.Collect() method. The garbage collector checks to see if there are any objects in the heap that are no longer being used by the application(checking the references exist for an object).
class Person
{
public Person() //Constructor
{
Console.WriteLine("Person {0} constructor", GetHashCode());
}
~Person() //Destructor
{
Console.WriteLine("Person {0} finalizer", GetHashCode());
}
}
class GCDemo
{
static void Main(string[] args)
{
Person p = new Person(); // The constructor is invoked here.
p = null;
Console.WriteLine("The object p will not be used anymore ");
Person pc = new Person(); // The constructor is invoked here.
pc = null;
Console.WriteLine("The object pc will not be used anymore ");
GC.Collect(); // Force to collect.
Console.WriteLine("Now the carbage collection is called forcefully...");
GC.WaitForPendingFinalizers(); // Wait until it finish
}
}
Output:
Person 33156464 constructor
The object p will not be used anymore
Person 15645912 constructor
The object pc will not be used anymore
Now the carbage collection is called forcefully...
Person 33156464 finalizer
Person 15645912 finalizer
Conclusion:
- Destructor methods are called after the garbage collection method executed.
- Garbage collector destroys both person objects because reference "p" and "pc" is null.
Labels:
C#,
Memory Management
Memory Usage Reduces On Application Minimize
Everseen memory usage of a program go down as soon as it’s minimized. Well how to achieve this programmatically(VB.NET and C#).
Using the anyone of the following API function we can reduce memory usage.
SetProcessWorkingSetSize( GetCurrentProcess(), -1, -1 );
or
EmptyWorkingSet( GetCurrentProcess() );
Sample Code:
C#:
public class MemoryManagement
[DllImport("kernel32.dll")]
public static extern bool SetProcessWorkingSetSize( IntPtr proc, int min, int max );
public void ReleaseMemory()
{
GC.Collect() ;
GC.WaitForPendingFinalizers() ;
}
}
public class MemoryManagement
Private Declare Auto Function SetProcessWorkingSetSize Lib "kernel32.dll" (ByVal procHandle As IntPtr, ByVal min As Int32, ByVal max As Int32) As Boolean
Friend Sub ReleaseMemory()
Try
Catch
End Try
End Sub
end class
Using the anyone of the following API function we can reduce memory usage.
SetProcessWorkingSetSize( GetCurrentProcess(), -1, -1 );
or
EmptyWorkingSet( GetCurrentProcess() );
Sample Code:
C#:
public class MemoryManagement
{
[DllImport("kernel32.dll")]
public static extern bool SetProcessWorkingSetSize( IntPtr proc, int min, int max );
public void ReleaseMemory()
{
GC.Collect() ;
GC.WaitForPendingFinalizers() ;
if(Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1) ;
}
{
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1) ;
}
}
}
VB.NET :
public class MemoryManagement
Private Declare Auto Function SetProcessWorkingSetSize Lib "kernel32.dll" (ByVal procHandle As IntPtr, ByVal min As Int32, ByVal max As Int32) As Boolean
Friend Sub ReleaseMemory()
Try
GC.Collect()
GC.WaitForPendingFinalizers()
If Environment.OSVersion.Platform = PlatformID.Win32NT Then
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1)
End If
GC.WaitForPendingFinalizers()
If Environment.OSVersion.Platform = PlatformID.Win32NT Then
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1)
End If
Catch
End Try
End Sub
end class
Labels:
C#,
Memory Management,
VB.NET
Subscribe to:
Posts (Atom)