July 10, 2009

Virtual, Override, New keywords in .net

The “override” keyword is used to hide the definition of a method in the derived class. Whereas the new keyword is used to provide a new definition for the method.
The "virtual “ keyword make the method to be overridden in the derived class.
Let us understand the difference like this. When we declare a parent object and instantiate it with the child object, and when we use this object to call overridden method then the child class method will be executed. And in case of new method the parent class method will be called.
So in case of overriding , this is called as polymorphism.
But if we have declared child object and instantiating it with the same, and then if we make a call to the method with the object then in both the cases the derived method will be invoked. Lets see this with an example.



Here we have one base class named “Base”, which has two member methods, “one()” that is marked as virtual. Other is “two()” which is a non-virtual method.



Now we have a class “Child” which is derived from the “Base”. It has overriden method “one()” and a new method “two()”.



In the Main() method we have declared “b” as Base type and instantiated it with the Child type. And “c” is declared and instantiated as “Child” type.
When one execute this piece of code, following will be the observation:

Child overridden method one
Child new method two
Child overridden method one
Base non virtual method TWO


Thus it can be seen, our point is very clearly demonstrated with this example.

.Net Portable Executable File Format

A Windows executable, EXE or DLL must conform to the PE(Portable Executable) file format which is derivative of the Microsoft’ Common Object File Format(COFF). The Windows OS knows how to load and execute DLLs and EXEs because it understands the PE format. So any compiler that wants to generate windows executables must conform to the PE format.
In Standard Windows PE files we have two sections one is PE/COFF headers that reference the contents in the PE file, while the other section holds the number of native image sections, including .data, .rdata, .rsrc and .text sections. Microsoft has added a few sections to the standard PE file to provide support to the CLR. The CLR manages and understands these new sections.



As shown in the figure above, in .net PE file we have CLR headers and CLR data section in addition to the standard ones. The CLR header indicates that it is a .net executable, while CLR data holds the metadata and the IL(Intermediate language) that determine how the program needs to be executed.
This can be proved using tool dumpbin.exe
Just run “dumpbin.exe” on the command prompt followed by the executable file name with switch /all.
e.g : dumpbin.exe test.exe /all
it will illustrate the components of the PE

July 9, 2009

Garbage Collection

MEMORY MANAGEMENT :

In .net the memory management is handled by the CLR by means of Garbage Collector automatically . it solves the developers problem of keeping track of all the references and later freeing them when they are not in use. Which is a tedious task is done manually and if not done accurately can lead to memory leaks.

The Garbage Collection Algorithm.
Note: Roots identify storage locations, which refer to the objects on the managed heap or to objects that are set to null.

This algorithm involves the two phases/steps (mark and compact).
In the mark phase , aim is to identify the memory to be claimed and mark them. As the collector starts running it starts from the root object (usually global or static object pointers) that is identified by the JIT compiler and then traverses to the object chain. It traces the objects and add to the graph.
Examples of root objects :
• Global and static object pointers
• Any local variable/parameter pointers on a thread’s stack.
• Any CPU registers containing pointers in the managed heap.
The list of the active root is maintained by the JIT compiler and CLR and is made accessible to the garbage collector.
The GC starts from the live references/roots and walk through all the objects building a graph of all the objects that are reachable. It stops to traverse the path as soon as it encounters an object that is already present in the graph. Hence, doesn’t go through the objects more then once, and prevents the infinite loops in case of circular linked lists of objects, thus handling cycles properly. Once all the roots are marked, the graph has the set of all the objects that somehow reachable, while all the other objects which are not reachable are considered as garbage.
Now comes the second phase into picture i.e, “compact” phase. Here all the live objects are moved to the bottom of the heap , leaving the contiguous free memory space. Moving the objects invalidate all pointers , so the garbage collector, modifies the application’s roots to point to the new locations. Also it takes into account those objects that hold pointers to other object and correct that.
After all the garbage has been identified, all the non-garbage has been compacted, and all the non-garbage pointers have been fixed-up, a pointer is positioned just after the last non-garbage object to indicate the position where the next object can be added.



As can be seen from the above figure , the objects B,C are not referenced by any of the roots. So they are considered as the garbage and will be compacted.
To improve the performance CLR classifies the memory heap in 3 generations . This classification is made on the basis of the fact that newly created object tend to have short life. While older the object longer it will survive. There is no predefined time for a garbage collector to run. When the memory heap reaches a threshold then only it runs to release memory from the unused / non-referenced resources.



GC Generations Fig A
When initialized the heap has no objects. All new objects added to the heap are in Generation 0, once the heap gets filled up, the GC is invoked. As most of the objects are short lived only small number will survive the first collection. Those which survive the collection are moved to the Generation 1 level. So we have compact free space in Generation 0. When the next time GC is invoked, again the new survived objects are promoted to generation 1 while the generation 1 objects are moved to the generation 2. So as the objects mature in there present generation they are promoted to the next generation with each garbage collection.