Common Language Runtime

Microsoft Silverlight will reach end of support after October 2021. Learn more.

The common language runtime is the foundation of the .NET Framework. It is responsible for managing code execution at run time, and provides core services such as compilation, memory management, thread management, code execution, enforcement of type safety, and code safety verification. Compilers target the common language runtime, which defines the basic data types available to application developers. Because it provides a managed environment for code execution, the common language runtime enhances developer productivity and contributes to the development of robust applications.

This topic provides an overview of the following features of the common language runtime:

  • Memory management.

  • The common type system.

Memory Management

The common language runtime's garbage collector manages the allocation and release of memory for an application. For developers, this means that you do not have to write code to perform memory management tasks when you develop managed applications. Automatic memory management can eliminate common problems, such as forgetting to free an object and causing a memory leak, or attempting to access memory for an object that has already been freed.

When you initialize a new process, the runtime reserves a contiguous region of address space for the process. This reserved address space is called the managed heap. The managed heap maintains a pointer to the address where the next object in the heap will be allocated. Initially, this pointer is set to the managed heap's base address. All reference types are allocated on the managed heap. When an application creates the first reference type, memory is allocated for the type at the base address of the managed heap. When the application creates the next object, the garbage collector allocates memory for it in the address space immediately following the first object. As long as address space is available, the garbage collector continues to allocate space for new objects in this manner.

Allocating memory from the managed heap is faster than unmanaged memory allocation. Because the runtime allocates memory for an object by adding a value to a pointer, it is almost as fast as allocating memory from the stack. In addition, because new objects that are allocated consecutively are stored contiguously in the managed heap, an application can access the objects very quickly.

In addition to allocating memory, the garbage collector's optimizing engine determines the best time to perform a collection based on the allocations being made. When the garbage collector performs a collection, it releases the memory for objects that are no longer being used by the application.

The Common Type System

Because the runtime, rather than an individual language compiler, defines the available base types, developer productivity is enhanced. Programmers can write applications in their development language of choice, yet take full advantage of the runtime, the class library, and components written in other languages by other developers. Provided that a language compiler targets the .NET Framework and the common language runtime, components developed with that compiler can usually be accessed from applications developed in other languages. The common type system helps to realize the goal of language independence; developers can focus on developing an application in their language of choice, and can draw on libraries and components regardless of the language in which they were written.

The common type system supports two general categories of data types:

  • Value types

    Value types directly contain their data, and instances of value types are either allocated on the stack or allocated inline in a structure. Value types can be built-in (implemented by the runtime), user-defined, or enumerations. For a list of built-in value types, see the .NET Framework Class Library for Silverlight.

  • Reference types

    Reference types store a reference to the value's memory address and are allocated on the heap. Reference types can be self-describing types, pointer types, or interface types. The type of a reference type can be determined from values of self-describing types. Self-describing types are further split into arrays and class types. The class types are user-defined classes, boxed value types, and delegates.

Variables that are value types each have their own copy of the data; therefore, operations on one variable do not affect other variables. Variables that are reference types can refer to the same object; therefore, operations on one variable can affect the same object referred to by another variable.

Except for interfaces, all types derive from the System.Object base type.

See Also