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

发布于 2024-09-12 06:17:54 字数 168 浏览 0 评论 0原文

在看到和听到了很多有关托管和非托管代码的信息后,我知道唯一的区别是托管是关于 CLR 的,而非托管是在 CLR 之外的,这让我非常想详细了解它。托管和非托管代码、内存和大小到底是什么?

我用 C# 编写的代码如何成为非托管的,而这是 C# 代码,以及大小的内存如何变得非托管。一个例子和一点见解会很有帮助。

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

雪花飘飘的天空 2024-09-19 06:17:54

简短回答:

  • 托管代码是您编写并编译为 .NET 的 .NET 代码(VB.NET、C# 等)CIL
  • 非托管代码是不在 .NET 下编译为直接机器代码的代码。

长答案:

什么是托管代码?

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

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

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

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

什么是非托管代码?

非托管代码是在 Visual Studio .NET 2002 发布之前用来编写的代码。 Visual Basic 6、Visual C++ 6,哎呀,即使是您硬盘上可能仍在使用的 15 年历史的 C 编译器,也都生成了非托管代码。它直接编译为机器代码,在您编译它的机器上运行 - 以及在其他机器上运行,只要它们具有相同的芯片或几乎相同的芯片。它没有从不可见的运行时获得安全或内存管理等服务;它从操作系统中获取它们。重要的是,它通过请求(通常是调用 Windows SDK 中提供的 API)从操作系统显式获取它们。最近的非托管应用程序通过 COM 调用获取操作系统服务。

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

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

原生代码怎么样?

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

本机代码一词的另一个用途是描述 JIT 编译器的输出,即在运行时实际运行的机器代码。它是托管的,但它不是 CIL,而是机器代码。因此,不要仅仅假设本机 = 非托管。

来源

Short answer:

  • 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.

Long answer:

What Is Managed Code?

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.

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 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..

What Is Unmanaged Code?

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.

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.

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.

What about Native Code?

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.

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.

(Source)

写给空气的情书 2024-09-19 06:17:54

这可能是一个很长的答案,讨论 C 编译器生成的机器代码与 JIT 编译器从托管程序生成的机器代码之间的许多细微差别。足够长的时间确实需要一本书,但这样的书已经写好了。例如,杰弗里·里克特(Jeffrey Richter)的任何作品。

我将保持简短明了,因为所有这些细微的差异都归结为一条规则:

托管代码是从垃圾收集堆中分配内存的代码。

This could be a very long answer, talking about the many subtle differences between machine code generated by a C compiler vs the machine code generated by the JIT compiler from a managed program. Long enough to really require a book, but such books have already been written. Anything by Jeffrey Richter for example.

I'll keep it short and snappy, because all those subtle differences boil down to the One Rule:

Managed code is code that allocates memory from the garbage collected heap.

倾`听者〃 2024-09-19 06:17:54

托管代码运行在应用程序域中,非托管代码运行在操作系统进程中。

Managed Code runs in Application Domain and Unmanaged Code runs under process of operating system.

铁轨上的流浪者 2024-09-19 06:17:54

通常,托管代码比同等编写良好的非托管(本机)代码具有更高的运行时内存占用量。

Generally managed code has higher runtime memory footprint than equivalent well-written non-managed (native) code.

诗化ㄋ丶相逢 2024-09-19 06:17:54

非托管代码:-

1.在.NET、Framework之外开发的代码称为非托管代码。

2.不在CLR控制下运行的应用程序被称为非托管应用程序,可以使用某些语言(例如C++)来编写此类应用程序,例如访问操作系统的低级功能。与 VB、ASP 和 COM 代码的后台兼容性是非托管代码的示例。

3.非托管代码在包装类的帮助下执行。

4.Wrapper类有两种类型:CCW(COM Callable Wrapper)和RCW(Runtime Callable Wrapper)。

5.Wrapper借助CCW和RCW来掩盖差异。
托管代码
应用程序域中的资源是托管代码。域内的资源速度更快。

托管代码
1.在.NET框架中开发的代码称为托管代码。
该代码由 CLR 在托管代码执行的帮助下直接执行。任何用 .NET Framework 编写的语言都是托管代码。

2.托管代码使用CLR,CLR通过管理内存、处理安全性、允许跨语言调试等来管理您的应用程序。

Unmanaged Code :-

1.The code, which is developed outside .NET, Framework is known as unmanaged code.

2.Applications that do not run under the control of the CLR are said to be unmanaged, and certain languages such as C++ can be used to write such applications, which, for example, access low - level functions of the operating system. Background compatibility with code of VB, ASP and COM are examples of unmanaged code.

3.Unmanaged code is executed with help of wrapper classes.

4.Wrapper classes are of two types: CCW (COM Callable Wrapper) and RCW (Runtime Callable Wrapper).

5.Wrapper is used to cover difference with the help of CCW and RCW.
Managed Code
The resource, which is with in your application domain is, managed code. The resources that are within domain are faster.

Managed Code
1.The code, which is developed in .NET framework, is known as managed code.
This code is directly executed by CLR with help of managed code execution. Any language that is written in .NET Framework is managed code.

2.Managed code uses CLR which in turns looks after your applications by managing memory, handling security, allowing cross - language debugging, and so on.

本王不退位尔等都是臣 2024-09-19 06:17:54

直接在操作系统下运行的应用程序称为

<块引用>

非托管应用

,而在 .net 框架下运行的应用程序称为

<块引用>

托管应用

Apps Which is directly run under the OS are known as

un-managed apps

whereas apps which run under the .net framework are known as

managed apps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文