Common Language Runtime, CLR

The Common Language Runtime (CLR), the virtual machine component of Microsoft's .NET framework, manages the execution of .NET programs. A process known as just-in-time compilation converts compiled code into machine instructions which the computer's CPU then executes. The CLR provides additional services including memory management, type safety, exception handling, garbage collection, security and thread management. All programs written for the .NET framework, regardless of programming language, are executed by the CLR. All versions of the .NET framework include CLR.



CLR Hosting

호스트(host)란 다른 장치 또는 프로그램에 서비스를 제공하는 장치나 프로그램을 말하고, 호스팅(hosting)한다는 말은 호스트로부터 서비스를 제공받는 행위를 말한다.

MS는 CLR을 DLL에 포함된 일종의 COM 서버의 형태로 구현했다. 어느 윈도우 프로그램이라도 CLR을 호스팅할 수 있다. C++와 같은 unmanaged 코드에서 .NET 기능을 사용하고자 할 때, CLR의 COM 서버 인스턴스를 생성하기 위해서는 MsCorEE.dll에 구현되어 있는 CorBindToRuntime()와 같은 함수를 사용해야 한다. CorBindToRuntime() 함수 호출 시 어느 프레임워크 버전의 CLR을 생성할 건지를 인자로 전달해야 한다. (참고로 CorBindToRuntime()는 .NET 4.0에서 deprecated 되었다. .NET 4.0 버전부터는 CLRCreateInstance() 함수와 ICLRMetaHost 인터페이스의 GetRuntime 메소드를 이용할 수 있다.)

그리고, 프로세스는 한 가지 버전의 CLR만 로드할 수 있는데, 한번 로드된 CLR은 프로세스를 종료해야만 다음에 다른 버전의 CLR을 로드할 수 있다.


AppDomain

운영체제에서는 프로세스가 application의 경계가 되었고, .NET에서는 실행공간을 나누기 위하여 AppDomain이라는 개념을 사용하고 있다. 하나의 프로세스는 여러 개의 AppDomain을 가질 수 있고, 개수의 제한이 없으며, 생성 및 언로드가 가능하다.

.NET application이 시작되면, CLR은 기본적으로 default AppDomain을 생성하고 그 안에서 application을 실행시킨다. 두 application이 동일한 어셈블리를 참조하고 있다면 각 application은 각자의 AppDomain으로 어셈블리를 로딩하고 객체도 각자의 AppDomain 안에서 따로 생성한다. AppDomain 간에는 객체의 인스턴스 멤버뿐만 아니라 정적 멤버도 공유가 되지 않는다. 완전히 독립된 실행공간이다.


References