Site Feed

Friday, October 29, 2004

.NET Assembly Structure – Part 1

In this blog, I will explain the structure of .net assembly. What is an assembly? Prior to explaining what is an assembly, let me give you all a brief preface on where it begins.

Microsoft, the software giant has gone one step ahead of its competitors to bring out the language independent software development environment. Microsoft has brought out their proprietary .net languages such as C#, VB.NET, C++.net, etc. for diverse skilled developers. These high level languages are in reality an abstraction to an intermediary language, which Microsoft has designed, called MSIL.

The MSIL is structured in such a way that the .NET framework can turn them into native code on any Windows platform using a windows specific execution engine called CLR or Common Language Runtime. Microsoft has compilers written for C#, VB.NET etc. which churns the code into the intermediary language or MSIL.

MSIL, what we can from now on call an assembly, is an executable file when it comes under the hood of .NET runtime. The .NET runtime loads the assembly into a process and does a Just in Time compilation. JIT? This is that feature of the CLR which converts the MSIL into native code. The native code is the instructions which the OS understand and the machine can execute. Here is the catch… we have a Windows specific .NET execution engine. Now if we have .NET execution engine specific to other Operating Systems such as Linux or UNIX, we can then call .NET to be platform independent too. There is definitely an effort in this direction from Open source community. This is called Mono, a .net run time for Linux. Microsoft might have their own justification behind not bringing out runtime for other operating systems, but then, that is another topic for debate.

... to be continued.

Wednesday, October 27, 2004

Garbage Collection

Garbage collection is a mechanism by which the CLR reclaims inaccessible (or unreachable) memory allocated to an object. Question, when does an object become unreachable? This can be determined by the references that an application has to this object at any given point of time. This can happen in different ways. An object can go out of scope or if the reference to an object can be explicitly set to null rendering the references to be invalid. Thus, the Garbage Collector has to keep a track of all objects that needs to be garbage collected.

How does this happen? Let me start by asking to when do the GC first gets invoked? The GC first gets invoked as soon as the first object for the application gets generated on the heap when starting. Once started, the garbage collection process runs on a thread that is separate from the application main thread. This thread is completely inaccessible to the main thread in the application thus providing complete security to the garbage collection thread. Every time an object is allocated a memory space using the new key word, the garbage collection runs.

When the garbage collection runs, it first mark all the objects created in the memory to be garbage collected. Then it looks out for the references that are still alive on the object. It examines the references on an object and if found, it de-marks the object. Then the garbage collector reclaims all memory which was allocated to objects that are no more referenced in the managed code. This process is called mark-and-sweep.