When you program using a programming model, or framework, it's a good idea to understand that framework before...
you begin. And the .NET Framework is more than a little complex, with many components that have to be understood. This tip, excerpted from InformIT, discusses the purposes and attributes of the common language runtime, one of the fundamental portions of the .NET Framework.
The CLR is the execution environment provided by the Microsoft .NET Framework. It provides many services such as:
- Automatic garbage collection
- Code access security
- Simplified versioning
- Simple and reliable deployment
- Deep cross-language interoperability
- Debugging across different languages
Because the CLR manages the code execution, all the code that is targeted for the CLR is known as managed code. Managed code emits metadata along with the executable. This metadata is used to describe the types (classes) and members used in the code, along with all the external references used in executing the code. The CLR uses this metadata to load the classes during execution and resolve method invocations during runtime.
The CLR provides automatic garbage collection of the objects that have been loaded into memory. All objects that are created via the new operator are allocated memory on the heap. A program can allocate as many objects as are required by the program logic. However, when an object is no longer required, there must be some mechanism to free up the memory that was occupied by the object.
This is accomplished in the CLR via a program called garbage collector, which collects all objects in memory that have no references. This program runs as a low-priority thread in the background process and collects all unreferenced objects. Because memory management is automatic, the chances for memory leaks in the program are minimized.
However, the time when garbage collector would actually release the objects from the memory is not known. This concept is known as nondeterministic garbage collection because it cannot be determined in advance when the objects would be released from memory.
If sufficient memory is not available for creating new objects, the CLR throws an exception that can be caught and gracefully handled by the application.
Code Access Security (CAS), as the name suggests, is used to control the access that the code has to system resources. The CLR has a runtime security system. Administrators can configure policy settings by specifying the resources that can be accessed by the code.
A call stack is created that represents the order in which the assemblies get called. The CLR's security system walks the stack to determine whether the code is authorized to access the system resources or perform certain operations. If any caller in the call stack does not have the requisite permission to access the specific system resources, a security exception is thrown by the CLR.
Simplified versioning is another feature provided in the .NET Framework. It supports versioning and also provides for side-by-side execution of different versions of the same component. The specific versions of the assembly and the dependent assemblies are stored in the assembly's manifest. The copies of the same assembly that differ only in version numbers are considered to be different assemblies by the CLR. Assemblies are explained in more detail in the later sections.
Simplified deployment is one of the features provided in the .NET Framework. The most important point to mention is that .NET components do not need to be registered in the Windows registry. All code generated in the .NET Framework is self-describing because assemblies contain the manifest and metadata information. This information contains all the data about the dependencies of the assembly and the specific versions of the components that these assemblies would use at execution time; therefore, multiple versions of the same components can coexist. The CLR enforces the versioning policy.
Cross-language interoperability is an important feature, and it was one of the design goals of the .NET Framework. This feature is possible because of the CTS and CLS. The CTS is explained in more detail in the next subsection.
Visual Studio .NET allows for debugging across an application consisting of different languages targeted for the CLR. In fact, the IDE also allows for debugging an application in which managed code interacts with unmanaged code.
CLR ensures that performance of the code execution is optimized. Compiled code is stored in cache. When the same code is called next time, this code is loaded into memory from cache. This advantage stands out more in the case of ASP.NET applications than for ASP applications. ASP code was interpreted every time an ASP page was requested. In ASP.NET, the code is compiled only once when the page is requested for the first time. This ensures that performance is optimized.
The .NET Framework also provides some classes for tracking the performance of the .NET applications. These classes are known as performance counters. The .NET Framework provides performance counters for getting information on exception handling, interoperation with unmanaged code, loading and unloading code into memory, locking and threading, memory, networking operations, and so on. These performance counters help to fine-tune the performance of the .NET applications.
To read the entire article from which this tip is excerpted, click over to InformIT. You have to register there, but the registration is free.