托管和非托管代码,内存和大小有什么区别?

问题描述:

看了很多关于托管和非托管代码的信息,并且知道唯一的区别是托管是关于CLR的,而非托管是在CLR之外的,这让我真的很想知道它的细节。

After seeing and listening a lot regarding managed and unmanaged code, and knowing the only difference is that managed is about CLR and un-managed is outside of the CLR, it makes me really curious to know it in detail. What is it all about, managed and unmanaged code, memory and size?

这是C#代码,如何对我在C#中编写的代码进行非托管,以及如何存储这些代码,这又是什么呢?大小变得不受管理。一个例子和一点见识会有所帮助。

How can code I write in C# be unmanaged while this is C# code and how does a memory of size becomes unmanaged. An example and a little insight would be helpful.

简短答案:


  • 托管代码是您编写并编译到.NET的.NET代码(VB.NET,C#等)。 CIL

  • 非托管代码是不在.NET下的代码,

  • Managed code is .NET code (VB.NET, C# etc.) that you write and compile to .NET CIL.
  • Unmanaged code is code that is not under .NET that compiles to direct machine code.

长答案:

托管代码是Visual Basic .NET和C#编译器创建的。它编译为中间语言(IL),而不是可以直接在您的计算机上运行的机器代码。 CIL与描述您创建的代码的类,方法和属性(例如安全要求)的元数据一起保存在一个名为程序集的文件中。该程序集是.NET世界中一站式采购部署单元。您将其复制到另一台服务器上以在其中部署程序集,并且通常,复制是部署中唯一需要的步骤。

Managed Code is what Visual Basic .NET and C# compilers create. It compiles to Intermediate Language (IL), not to machine code that could run directly on your computer. The CIL is kept in a file called an assembly, along with metadata that describes the classes, methods, and attributes (such as security requirements) of the code you've created. This assembly is the one-stop-shopping unit of deployment in the .NET world. You copy it to another server to deploy the assembly there—and often that copying is the only step required in the deployment.

托管代码在公共语言运行时中运行。运行时为正在运行的代码提供了多种服务。在正常情况下,它将首先加载并验证程序集,以确保CIL正常。然后,在调用方法时,运行时将运行时安排它们编译为适合于运行程序集的机器的机器代码,并缓存该机器代码以在下次调用该方法时使用。 (这被称为Just In Time或JIT编译,或者通常只是Jitting。)

Managed code runs in the Common Language Runtime. The runtime offers a wide variety of services to your running code. In the usual course of events, it first loads and verifies the assembly to make sure the CIL is okay. Then, just in time, as methods are called, the runtime arranges for them to be compiled to machine code suitable for the machine the assembly is running on, and caches this machine code to be used the next time the method is called. (This is called Just In Time, or JIT compiling, or often just Jitting.)

在程序集运行时,运行时将继续提供安全性,内存管理等服务。 ,线程等。该应用程序由运行时管理。

As the assembly runs, the runtime continues to provide services such as security, memory management, threading, and the like. The application is managed by the runtime.

Visual Basic .NET和C#只能生成托管代码。如果您正在使用这些应用程序,那么您将在编写托管代码。如果愿意,Visual C ++ .NET可以生成托管代码:创建项目时,选择名称以.Managed。开头的应用程序类型之一,例如.Managed C ++应用程序。.

Visual Basic .NET and C# can produce only managed code. If you're working with those applications, you are making managed code. Visual C++ .NET can produce managed code if you like: When you create a project, select one of the application types whose name starts with .Managed., such as .Managed C++ application..

非托管代码是在发布Visual Studio .NET 2002之前要使用的代码。 Visual Basic 6,Visual C ++ 6,哎呀,即使是15岁的C编译器,您可能仍然在硬盘驱动器上产生了所有产生的非托管代码。它直接编译为在您编译该机器的计算机上以及在其他具有相同或几乎相同芯片的机器上运行的机器代码。它没有从不可见的运行时获得安全或内存管理等服务;它是从操作系统中获得的。而且重要的是,它通常是通过调用Windows SDK中提供的API来从操作系统中明确要求它们的。

Unmanaged code is what you use to make before Visual Studio .NET 2002 was released. Visual Basic 6, Visual C++ 6, heck, even that 15-year old C compiler you may still have kicking around on your hard drive all produced unmanaged code. It compiled directly to machine code that ran on the machine where you compiled it—and on other machines as long as they had the same chip, or nearly the same. It didn't get services such as security or memory management from an invisible runtime; it got them from the operating system. And importantly, it got them from the operating system explicitly, by asking for them, usually by calling an API provided in the Windows SDK. More recent unmanaged applications got operating system services through COM calls.

与Visual Studio中的其他Microsoft语言不同,Visual C ++可以创建非托管应用程序。当您创建一个项目并选择名称以MFC,ATL或Win32开头的应用程序类型时,您正在创建一个非托管应用程序。

Unlike the other Microsoft languages in Visual Studio, Visual C++ can create unmanaged applications. When you create a project and select an application type whose name starts with MFC, ATL, or Win32, you're creating an unmanaged application.

这可能会引起一些混乱。 :当您创建.Managed C ++应用程序时,生成产品是扩展名为.exe的CIL的程序集。当您创建MFC应用程序时,构建产品是Windows可执行文件的本机代码,其扩展名为.exe。这两个文件的内部布局完全不同。您可以使用中间语言反汇编程序ildasm来查看程序集内部并查看元数据和CIL。尝试将ildasm指向一个非托管的exe,您会被告知它没有有效的CLR(公共语言运行时)标头,并且无法反汇编-扩展名相同,文件完全不同。

This can lead to some confusion: When you create a .Managed C++ application., the build product is an assembly of CIL with an .exe extension. When you create an MFC application, the build product is a Windows executable file of native code, also with an .exe extension. The internal layout of the two files is utterly different. You can use the Intermediate Language Disassembler, ildasm, to look inside an assembly and see the metadata and CIL. Try pointing ildasm at an unmanaged exe and you'll be told it has no valid CLR (Common Language Runtime) header and can't be disassembled—Same extension, completely different files.

短语本机代码在两个上下文中使用。许多人将其用作非托管代码的代名词:使用较旧工具构建的代码,或在Visual C ++中有意选择的代码,它们不在运行时运行,而是在计算机上本地运行。这可能是一个完整的应用程序,也可能是一个使用COM Interop或PInvoke从托管代码中调用的COM组件或DLL,这两个功能强大的工具可确保您在迁移到新世界时可以使用旧代码。我更喜欢说.unmanaged代码。出于此含义,因为它强调代码不会获得运行时的服务。例如,托管代码中的代码访问安全性可防止从另一台服务器加载的代码执行某些破坏性操作。如果您的应用程序调用从另一台服务器加载的非托管代码,则不会获得这种保护。

The phrase native code is used in two contexts. Many people use it as a synonym for unmanaged code: code built with an older tool, or deliberately chosen in Visual C++, that does not run in the runtime, but instead runs natively on the machine. This might be a complete application, or it might be a COM component or DLL that is being called from managed code using COM Interop or PInvoke, two powerful tools that make sure you can use your old code when you move to the new world. I prefer to say .unmanaged code. for this meaning, because it emphasizes that the code does not get the services of the runtime. For example, Code Access Security in managed code prevents code loaded from another server from performing certain destructive actions. If your application calls out to unmanaged code loaded from another server, you won't get that protection.

短语本机代码的另一种用法是描述JIT编译器,即在运行时中实际运行的机器代码。它是托管的,但不是CIL,而是机器代码。因此,不要仅仅假设native = unmanaged。

The other use of the phrase native code is to describe the output of the JIT compiler, the machine code that actually runs in the runtime. It's managed, but it's not CIL, it's machine code. As a result, don't just assume that native = unmanaged.

来源