What is managed code?

原创 2007年09月14日 15:16:00
What is managed code?
Recently I have been working on pulling together some background information and I thought I'd share it here.  I certainly don't claim any of this is news, but at least google will help me find it next time I need it ;-)
What is managed code?
Managed code is code that has its execution managed by the .NET Framework Common Language Runtime. It refers to a contract of cooperation between natively executing code and the runtime. This contract specifies that at any point of execution, the runtime may stop an executing CPU and retrieve information specific to the current CPU instruction address. Information that must be query-able generally pertains to runtime state, such as register or stack memory contents.
The necessary information is encoded in an Intermediate Language (IL) and associated metadata, or symbolic information that describes all of the entry points and the constructs exposed in the IL (e.g., methods, properties) and their characteristics. The Common Language Infrastructure (CLI) Standard (which the CLR is the primary commercial implementation) describes how the information is to be encoded, and programming languages that target the runtime emit the correct encoding. All a developer has to know is that any of the languages that target the runtime produce managed code emitted as PE files that contain IL and metadata. And there are many such languages to choose from, since there are nearly 20 different languages provided by third parties – everything from COBOL to Camel – in addition to C#, J#, VB .Net, Jscript .Net, and C++ from Microsoft.
Before the code is run, the IL is compiled into native executable code. And, since this compilation happens by the managed execution environment (or, more correctly, by a runtime-aware compiler that knows how to target the managed execution environment), the managed execution environment can make guarantees about what the code is going to do. It can insert traps and appropriate garbage collection hooks, exception handling, type safety, array bounds and index checking, and so forth. For example, such a compiler makes sure to lay out stack frames and everything just right so that the garbage collector can run in the background on a separate thread, constantly walking the active call stack, finding all the roots, chasing down all the live objects. In addition because the IL has a notion of type safety the execution engine will maintain the guarantee of type safety eliminating a whole class of programming mistakes that often lead to security holes.
Contrast this to the unmanaged world: Unmanaged executable files are basically a binary image, x86 code, loaded into memory. The program counter gets put there and that’s the last the OS knows. There are protections in place around memory management and port I/O and so forth, but the system doesn’t actually know what the application is doing. Therefore, it can’t make any guarantees about what happens when the application runs.
版权声明:本文为博主原创文章,未经博主允许不得转载。 举报


Managed(托管), Unmanaged(非托管), Native: What Kind of Code Is This?

With the release of Visual Studio .NET 2003 (formerly known as Everett) on April 24th, many develope...

[Quora] What is the most elegant line of code you've seen?

@Murtaza Aliakbar: // Counting # bits set in 'v', Brian Kernighan's way for (c = 0; v; c++) v ...



What is “:-!!” in C code?

原文: http://stackoverflow.com/questions/9229601/what-is-in-c-code ==================================...

What Is Clean Code? – In Quotes

什么样的代码才是真正好的、整洁的代码?来看看大牛们怎么说。 Bjarne Stroustrup,C++之父: 引用 我喜欢优雅、高效的代码: ● 逻辑应该是清晰的,bug难以隐藏; ●依...

Sinking events from managed code in unmanaged C++

Download source code - 7.89 KB Introduction As time goes by, some parts of the big project I am ...

托管代码 managed code

什么是托管代码?       托管代码是一microsoft的中间语言,他主要的作用是在.NET   FRAMEWORK的CLR执行代码前去编译源代码,也就是说托管代码充当着翻译的作用,源代码在...

Manual commit is not allowed over a Spring managed

Manual commit is not allowed over a Spring managed SqlSession Manual close is not allowed over a Sp...