Translate

Sunday, September 9, 2012

In C # Managing the Lifetime of Objects and Controlling Resources

In C # Managing the Lifetime of Objects and Controlling Resources

Garbage Collector

It is found that all the applications which we create use some resources.The resources fall into two broad categories:
1.managed resources that are handled by the common language runtime (CLR)
2.unmanaged resources that are maintained by the operating system outside the scope of the CLR.

A managed resource is typically an object based on a class defined by using a managed
language, such asC#.

Unmanaged resources include items implemented outside the Microsoft .NET Framework, which include such as Component Object Model (COM) components, file handles, database connections, and network
connections.
Managing resources is important in any applications development,that you develop.
Microsoft has provided “GC” which is called “Garbage Collector “ it facilities  by automatically reclaiming the resources by a managed object when it is no longer referenced by an applica
tion.Managed resources are handled by the .NET Framework garbage collector.However, unmanaged resources are not controlled by the garbage collector; it must take special steps to dispose of them properly and prevent them from being held longer than necessary.


Object life Cycle




An object has several distinct stages in its life cycle, which starts at creation and
ends in destruction. Creating an object is very simple; the new keyword to instantiate the new object. However, the process that you use to create an object is not really this simple. When we create a
new object, It follows the following steps:

Steps
1. A big block of memory will be allocated to hold the object.

2. The object is initialized. The block of memory is converted to an object.
The runtime handles the allocation of memory for managed objects.

Destroying an Object

When you have finished with an object, it can be destroyed.
We use destruction to reclaim any resources used by that object. Like creation,
destruction is a two-phase process:

1. The object is cleaned up; for example, by releasing any unmanaged resources
used by the application, such as file handles and database connections.

2. The memory used by the object is reclaimed.You can control only the first of these steps—cleaning up the object and releasing resources by implementing a destructor.
The CLR handles the release of memory used by managed objects

Using IDisposable Interface

The IDisposable interface defines a single method called Dispose, which takes do not take any
parameters. The Dispose method should release all of the unmanaged resources that an object owns. It should also release all resources owned by its base types by calling the Dispose method of the parent type.Visual C# includes constructs that ensure that resources are released in a timely
fashion for classes that implement the IDisposable interface.


Sample Demo

class Employee : IDisposable
{
bool isDisposed = false;
string name;
public Employee(string Name)
{
name = Name;
}
public void PaySalary()
{
if (!isDisposed)
{
Console.WriteLine("Employee {0} paid.", name);
}
else
{
throw new ObjectDisposedException("Employee already
disposed.");
}
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool IsDisposing)
{
if (IsDisposing)
{
isDisposed = true;
Console.WriteLine("Employee object disposed.");
}
GC.SuppressFinalize(this);
}
}

No comments:

Post a Comment