What is garbage collection
The process of releasing memory objects which are no longer used by an application in a .net environment is known as garbage collection. garbage collector helps the clr to perform better memory management. by default garbage collection is an automatic process which is being executed by the clr. by using gc.collect() method we can force clr to perform garbage collection
How does garbage collection work
The process of releasing memory objects which are no longer used by an application in a .net environment is known as garbage collection. garbage collector helps the clr to perform better memory management.
how garbage collector works.
garbage collector uses a managed heap to store objects. newly created object will be at the top of the heap (higher memory addresses) and older objects will be at the bottom.
based on the age of memory objects, garbage collector group objects into to three different groups which are generally known as generations. there will be 3 different generations. generation 0, generation 1, generation 2
generation 0 – all the newly created objects will be in generation 0. after a period of time objects which are still alive in gen 0 will be moved to generation 1.
generation 1 (gen1) – all the objects which were in gen 0 and have survived a series of garbage collection will be moved to gen 1
generation 2 (gen 2) – objects like global variables, static variables will be usually be in gen 3 because they will be alive till the application runs.
collecting the garbage
during a gc operation clr will pause the application execution
there are two types of collection
1) partial collection – it is considered to a very cheap operation. only objects in gen 0 will be released
2) full collection – gc will perform this operation in all generations and it is considered to a an expensive operation
after this process dead objects will be removed from the heap and live objects will be rearranged to fill the gaps. generations also will be updated after this process
How finalization affects performance
When the garbage collector runs objects will finalize will not be destroyed immediately. instead it will be moved a finalization queue. also the child objects which are referred to this object cannot be destroyed this time. after the garbage collection finalize thread will execute the finally method and will become dead. these dead objects will be removed only on the next garbage collection.
this is the reason finalization has a bad effect on performance
Can we force garbage collector to run
We can force the garbage collector to run using gc.collect() method
What is reflection
The method of getting metadata information like type, modules, methods information from an assembly is known as reflection. reflection classes are grouped inside system.reflection namespace. reflection is also used to dynamically invoke methods inside a class.
What are different types of jit
Just in time compiler is known as jit and there are three different types of jit in .net
1) enco jit - econo-jit compiles a method only when it is called while execution and after the execution it removes this compiled method from memory
2) pre – jit- pre – jit compiles the entire code to msil during the time of deployment. complete compilation happens in one cycle
3) normal jit - normal jit compiles a method only when it is called. the main difference between enco jit and normal jit is, normal jit caches the compiled code in memory which is known as jitted. when a method called again it will use this cached code from memory