Showing posts with label Garbage Collection. Show all posts
Showing posts with label Garbage Collection. Show all posts
Sunday, October 20, 2013
Sunday, June 9, 2013
Dispose Vs Finalize
Both Dispose
and Finalize are used to release
un-managed resources, however there are many differences between the way
Dispose and Finalize operates.
The Dispose() method should be explicitly defined in the class, and the program using the instance of the class should explicitly call the Dispose method.
Finalize is not explicitly defined, Finalize code is written in the destructor of the class, and is called automatically when Garbage Collection runs.
The Dispose() method should be explicitly defined in the class, and the program using the instance of the class should explicitly call the Dispose method.
Finalize is not explicitly defined, Finalize code is written in the destructor of the class, and is called automatically when Garbage Collection runs.
Finalize in C#
The Finalize
block in C# .Net is used to clear un-managed resources before an object is
destroyed. Classes / Objects which use un-managed resources should implement the
Finalize method and clear all the un-managed resources they use in the Finalize
block.
Unlike the Dispose()
method, Finalize does not have an explicit Finalize()
method in C#, instead the destructor of the class acts as the Finalize() block
for the class.
The following example shows on how to declare the Destructor/Finalize block for a class.
The following example shows on how to declare the Destructor/Finalize block for a class.
Advantages of using Statement in C#
We
know that classes using un-managed resources should implement the IDisposable interface, override the Dispose() method and clear all the un-managed
resources in the Dispose() method. Even if all these are done properly, what if
the code using the object does not call the Dispose() method, the un-managed
resources will remain in memory till Garbage
Collection runs and clears them.
Whenever we use a class which implements the IDisposable interface, we should make sure that we call the Dispose() method of the class after using the object of the class. But we might at time forget to call the Dispose() method and this will hold the um-managed resources in memory till Garbage Collection runs. The using statement helps us avoid these situations by calling the Dispose method automatically.
The following example explains on how to use the using statement.
Whenever we use a class which implements the IDisposable interface, we should make sure that we call the Dispose() method of the class after using the object of the class. But we might at time forget to call the Dispose() method and this will hold the um-managed resources in memory till Garbage Collection runs. The using statement helps us avoid these situations by calling the Dispose method automatically.
The following example explains on how to use the using statement.
Dispose & IDisposable in C#
The Dispose method in .Net is used to clear out un-managed resources like file stream, database connection etc once an objects goes out of scope.
In .Net resources are not clear immediately, when they go out of scope, instead they are maintained in the Managed Heap and wait for Garbage Collection to run and clear the resources. However network, resources like file stream and database connections wait till Garbage Collection runs and releases them, these resources are shared and should be freed as soon as possible for other objects to use them, hence .Net introduces a special method called the Dispose() method which can be used to clear all such un-managed resources.
Garbage Collection Improvements in .Net 4.5
The .Net framework 4.5 introduces a set of feature
improvements to Garbage Collection,
which helps it to perform more effectively. We know that the earlier versions
of Garbage Collection had some limitations like performance issues when a full Garbage Collection happens, fragmentation
issues with the large object heap etc. The Garbage Collection which comes with
.Net 4.5 addresses some of these issues.
The following are some of the key improvements to Garbage Collection which ships with .Net 4.5
The following are some of the key improvements to Garbage Collection which ships with .Net 4.5
Saturday, June 8, 2013
Limitations of Garbage Collection
The .Net Garbage
Collection offers a lot of advantages like frees the developers from having
to allocate and dispose memory for the objects they create in their programs,
using the most efficient way to allocate and dispose memory, etc.
Though there are many advantages of Garbage Collection, there are a few limitations in the way Garbage Collection operates; the following are some of the Limitations of Garbage Collection.
Though there are many advantages of Garbage Collection, there are a few limitations in the way Garbage Collection operates; the following are some of the Limitations of Garbage Collection.
Garbage Collection Generations
Garbage
Collection involves monitoring the Managed Heap, clearing off
objects which are not used and reclaiming the memory used by those objects to
make it available for the objects which gets created during the program
execution.
Garbage Collection doesn't remove unused objects from the heap randomly, doing so might cause an overhead of re-creating frequently used objects again and again, to overcome this Garbage Collection employs a logic called Generations.
The Managed Heap is organized into a set of Generations, allocation of objects into each of this generation depends on the type of the object and the life time of the object.
The Heap is organized into 3 Generations
Garbage Collection doesn't remove unused objects from the heap randomly, doing so might cause an overhead of re-creating frequently used objects again and again, to overcome this Garbage Collection employs a logic called Generations.
The Managed Heap is organized into a set of Generations, allocation of objects into each of this generation depends on the type of the object and the life time of the object.
The Heap is organized into 3 Generations
GC.Collect
We
know that Garbage Collection is automatic and is managed by the runtime, but
there might be situation when you want to explicitly force Garbage Collection to
happen from your applications. The GC.Collect()
method does exactly this, it forcefully initiates a Garbage Collection run.
In general it is not advisable to use GC.Collect() from your code, since the runtime by itself will determine the most optimal time and way to run Garbage Collection. However you can still use GC.Collect() if you are sure that there are no critical operations planned to happen for the next few seconds.
The following are some of the situations when you might want to call GC.Collect()
In general it is not advisable to use GC.Collect() from your code, since the runtime by itself will determine the most optimal time and way to run Garbage Collection. However you can still use GC.Collect() if you are sure that there are no critical operations planned to happen for the next few seconds.
The following are some of the situations when you might want to call GC.Collect()
When does Garbage Collection Run?
Garbage
Collection is completely controlled by the .Net runtime,
generally we need not force the Garbage Collector to run, as this will lead to degradation
in performance, but still you can force the Garbage Collector to run by calling
the GC.Collect method.
The .Net runtime decides on the best time to run the Garbage Collection by evaluating various parameters on the available memory and other factors, the following are some of the situations when the .Net runtime triggers Garbage Collection.
The .Net runtime decides on the best time to run the Garbage Collection by evaluating various parameters on the available memory and other factors, the following are some of the situations when the .Net runtime triggers Garbage Collection.
Advantages of Garbage Collector
The
Garbage Collector does automatic
memory management for application running in the .Net. The Garbage Collector
makes sure that it reclaims unused memory and provides it the new objects which
get created as the program runs.
We know that all the reference type objects are stored in the Heap and it does not get cleared by itself, the garbage collector monitors the state of the heap and removes unused objects to reclaim the memory used by those objects.
The following are the advantages of Garbage Collector.
We know that all the reference type objects are stored in the Heap and it does not get cleared by itself, the garbage collector monitors the state of the heap and removes unused objects to reclaim the memory used by those objects.
The following are the advantages of Garbage Collector.
Friday, June 7, 2013
What is a Garbage Collector?
The Garbage
Collector is a mechanism employed by the .Net runtime to manage the memory
of objects added to the managed heap. Variables and references stored in a
Stack Frame are cleared (popped) out once the method execution gets completed,
but this is not the case for Heaps, there is no mechanism to clear the objects
loaded into the Heap, this is where the Garbage collector comes into play.
Subscribe to:
Posts (Atom)