Sunday, 4 October 2015

Garbage Collection in .Net



Hi Friends,

Here I gave detailed explanation of garbage collection.

Below are the questions that would you get about garbage collection when you want to know about it.

1. What is garbage collection?

2. Who is responsible for doing this?

3. How does garbage collection happen?

What is garbage collection?

Garbage collection is the cleaning up of objects in the managed head which are no longer needed.

Who is responsible for doing garbage collection?

Garbage collection is the responsibility of the CLR.CLR does this on its own in regularly. Developer doesn’t need to write anything for this. However, there are some scenarios developer has to write code for cleaning up of the objects explicitly which will be explained in the later section of the post.

How does garbage collection happen?

I already mentioned before CLR is responsible for cleaning up of the unused objects regularly. Now you get doubts like How frequent Garbage collector runs and how does it determine objects to be cleaned up.

Before going to answer above questions let me explain Generations concept.

CLR which is responsible for running of garbage collector should check each and every object that has been created in the managed heap to check whether it’s still needed.

But if we observe properly there will be some objects which should be in the memory for long time for example in Windows Application, Main window object of the application will be needed in the memory until application is closed unlike objects created local to methods, so its waste of time for CLR to visit every object and later realizing that It can’t free them.

Hence CLR classifies objects into different generations like Generation 0,Generation 1 and Generation 2.

CLR visits Generation 0 objects more frequently than other 2.

Based on the object requirements objects are assigned to specific generations.

Generation 0: A newly created objected which has never been visited by garbage collector.

Generation 1: An objected which has survived garbage collection

Generation 2: Identifies an object that has survived more than one sweep of the garbage collector.

The garbage collector will investigate all the generation 0 objects first. If it can get the required amount of memory any surviving objects are marked as generation 1.

If even after cleaning up some generation 0 objects if still its need of some more space, garbage collector will go and check in generation 1.If still needed generation 2 objects are evaluated.

So it is clear that by assigning a generation value on the heap, most recently created, no longer needed objects are cleaned up first and older objects are not visited much for clean up.



Forcing Garbage Collection:

Garbage collector claims the memory on behalf of us.In some scenarios we have clean up memory explicitly.

1. You don’t want your application to be interrupted.

2. If you have created huge number of objects and you want clean up any space.

Cleaning up unmanaged objects:

Finalize() method is for cleaning unmanaged objects.

There could be cases where you might be calling unmanaged methods inside your C# code.

Any garbage collector before cleaning up managed resources creates object graph for unmanaged objects.

All the objects that have to run Finalizer are Finalizable Objects.

Destructors internally implement Finalize()

Cleaning up unmanaged memory with Dispose() method:

As we have already seen Finalizers are used to clean up the unmanaged objects when garbage collector is collected.

In case if you have large memory space that is being used by unmanaged resource and has to be cleaned you may not want to wait for Garbage collector.In this case we gave Dispose()\

In order to implement Dispose() need to implement IDisposable interface.















Building Finalize –Dispose Pattern:

In the above method we used Dispose(),If we forget to call Dispose() Unmanaged memory will never be cleaned up.

So we use Finalize Finalize Dispose pattern.In this we forget to call Dispose(),Finalize will be called.

Example code :









Please don't forget to share your feedback.

                

Friday, 2 October 2015

What are CLR,CLS and CTS?



Hi Friends,

Here is the explanation for CLR,CLS and CTS.

Common Language Runtime:

.Net provides environment called as Common Language Runtime to run the applications and provide some services.

Before explaining more about this let me explain where CLR comes into picture in the managed execution process.

Below are the steps involved in Managed execution.

To utilize the benefits of the runtime choose one or more compilers which target the Runtime.

2.Compilers convert your source code into Metadata + MSIL

JIT compiler or nGen.exe compilers converts the MSIL into native code depending on the system configuration after optimization.

Here comes CLR to provide environment to that enables execution to take place and provides services that can be used during the execution process.


As we see in the previous steps before coming to CLR we had MSIL + Metadata, CLR used metadata to locate the classes, layout the instance in memory and resolve method invocations.

It’s the responsibility of the CLR to support applications and components whose objects are across different languages. To fulfill its responsibilities it has CTS (Common Type System) to define how types are declared and used in CLR when I say CLR I mean any language that is targeted to CLR.

CLS (Common Language Specification): CLS has some requirements for CLS compliance. If your code conforms to these required then your code can be called from any other CLS compliant code.

Common Language Specification:

It’s set of rules that are to be followed by all the .Net CLR compliant languages.

It’s basically for interoperability between the .Net CLR compliant languages.

We can develop .Net application which can have components developed multiple languages.

To fully interoperable with different languages used, Objects must expose only common functionalities that are understood by all the languages.

So if the component that you have developed has the CLS compliant code, then it can be used by any other CLS compliant language.


Common Type System: As name itself indicates it defined how types are declared, used and managed in Common Language Runtime .It’s to common language integration support.

It has defined rules for languages that target CLR to ensure objects written in different languages can interact.

Friends,Please leave your feedback.