Garbage collection
Garbage collection is a technique to recover the memory consumed by some unused variable and objects. When you create an instance of an object, some memory is allocated for the object .But when the object is no longer usable, the memory remain occupied for the object. For example, we have created method, the method have two variable which hold two class instance. Now we call the method, the two variable consumes some space, now the method call is over, what will be about the memory, consume by the two variable ?
Garbage collection works to clean up the objects memory. It is a low priority threat , and it is always running. Garbage collection process gates higher priority,when memories almost full. Garbage collection works with higher priority to clean up the memory and let the other priority works smoothly. Garbage collection is a continuous work.Garbage collection find the reference tree before disposing any object, because an object could be reference to another object and system does not knows when any object will be called. In general case, an object containing high memory allocation, dispose method is written to dispose the object without garbage collection.
The dispose method free unmanage resources like files , database connection and com component. It belongs to IDdisposable Interface. Dispose method can be called . It is a pattern to dispose and object. This comes with two overload, Dispose(), Dispose(Boolean). Dispose method cleanup all the object, second overload, Dispose( Boolean) ,Boolean indicate the method is coming from dispose method or not.Here is the example of dispose method.
Here is some namespace , that provides Garbage Collection
1) Default
2) Forced
3)Optimized
Circular reference : An object can be referred to another object, another object can be
referred to another object. In such way, circular reference can be
formed. Object containing circular reference is very difficult to
dispose. Reference tree of a circular reference point to each other.
Garbage collection is wise enough to handle such type of problem.
Garbage collection dispose both object which
pointed to each other.
Garbage collection is a technique to recover the memory consumed by some unused variable and objects. When you create an instance of an object, some memory is allocated for the object .But when the object is no longer usable, the memory remain occupied for the object. For example, we have created method, the method have two variable which hold two class instance. Now we call the method, the two variable consumes some space, now the method call is over, what will be about the memory, consume by the two variable ?
public
class
student
{
}
public
class
school
{
}
public
class
show
{
void
GetFess()
{
student
obj = new
student();
school
obj1 = new
school();
}
}
Garbage collection works to clean up the objects memory. It is a low priority threat , and it is always running. Garbage collection process gates higher priority,when memories almost full. Garbage collection works with higher priority to clean up the memory and let the other priority works smoothly. Garbage collection is a continuous work.Garbage collection find the reference tree before disposing any object, because an object could be reference to another object and system does not knows when any object will be called. In general case, an object containing high memory allocation, dispose method is written to dispose the object without garbage collection.
The dispose method free unmanage resources like files , database connection and com component. It belongs to IDdisposable Interface. Dispose method can be called . It is a pattern to dispose and object. This comes with two overload, Dispose(), Dispose(Boolean). Dispose method cleanup all the object, second overload, Dispose( Boolean) ,Boolean indicate the method is coming from dispose method or not.Here is the example of dispose method.
public
class
Customer
: IDisposable
{
bool
disposed = false;
public
void
Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected
virtual
void
Dispose(bool
disposing)
{
if
(disposed)
return;
if
(disposing)
{
}
disposed
= true;
}
}
Garbage collection provided method call finalize .It free unmanage
resources like database connection, files and com . Finalize
method is held before it is destroyed. Example of finalized.
class
Student
{
public
Student()
{
Console.WriteLine("I
am in Student");
}
~Student()
{
Console.WriteLine("Destructor
of Student");
}
}
Here is some namespace , that provides Garbage Collection
- System.GC :
student
obj = new
student();
//
Return maximum number of genration
Console.WriteLine(GC.MaxGeneration);
//
do collection
GC.Collect();
//
do collection for 0 generation
GC.Collect(0);
//
do collection upto 1 generation
GC.Collect(0);
//
do collection upto 2 generation
GC.Collect(2);
//total
allocated bytes
Console.WriteLine(GC.GetTotalMemory(false));
- System.GCCollectionMode : Return behavior for a forced garbage collection , three mode
1) Default
2) Forced
3)Optimized
GC.Collect(0,
GCCollectionMode.Default);
GC.Collect(0,
GCCollectionMode.Forced);
GC.Collect(0,
GCCollectionMode.Optimized)
- System.GCNotificationStatus :
IndiCator
= GC.WaitForFullGCComplete();
if
(IndiCator == GCNotificationStatus.Succeeded)
{
//Do
some thiing
}
else
if
(IndiCator == GCNotificationStatus.Canceled)
{
//Do
some thiing
}
else
if
(IndiCator == GCNotificationStatus.Timeout)
{
//Do
some thiing
}
else
if
(IndiCator == GCNotificationStatus.Timeout)
{
//Do
some thiing
}
pointed to each other.
public
class
student
{
school
b;
public
student(school
b)
{
this.b
= b;
}
}
public
class
school
{
student
a;
public
school(student
b)
{
this.a
= b;
}
}
No comments:
Post a Comment