托管堆和非托管堆
什么是非托管堆?
我认为 CLR 管理的任何基于对象的内存都是托管堆,那么为什么我们要谈论非托管堆呢?
What is an unmanaged heap?
I thought any object based memory that the CLR manages was the managed heap, so why do we talk about an unmanaged heap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
想象一下,您使用 P/Invoke 调用 Win32 函数,并使用
malloc
分配一些内存。垃圾收集器看不到该内存 - 它是非托管的。当然,这可能是也可能不是您听到该术语的上下文 - 如果您能给我们举一些例子,我们也许能够为您提供更多帮助。
Imagine you call a Win32 function using P/Invoke, and that allocates some memory using
malloc
. The garbage collector has no visibility of that memory - it's unmanaged.That may or may not be the context in which you've heard the term, of course - if you could point us to some examples, we may be able to help you more.
根据 John Skeet 的说法 - 托管堆是 .net 将为您管理的堆,所有标准对象都是在其上创建的,您通常不需要太担心,因为它是托管的。
非托管
意味着您亲自分配内存,因此您亲自负责释放内存、自行管理内存并跟踪正在使用的内存。所以,是的,对象内存(在正常对象创建和销毁的意义上,从对象派生的东西)是被管理的。这是您需要担心的其他事情 - 非对象和为它们分配的内存。
As per John Skeet - the
managed
heap is the one that .net will manage for you, that all standard objects are created on, that you normally don't need to bother too much about because it is managed.unmanaged
means that you are personally allocating memory, and so you are personally responsible for deallocating it, managing it yourself, and keeping track of what is being used.So yes, object memory ( in the sense of normal object creation and destruction, things that derive from object ) is managed. It is the other stuff you need to worry about - non objects and memory allocated for them.
为了理解 .net 中的非托管堆,我们需要了解什么是托管堆。
在.net框架中,我们有垃圾收集器,它由公共语言例程初始化。在初始化期间,垃圾收集器分配一段内存来存储和管理由托管代码实例化的对象。该内存称为托管堆,而不是本机操作系统中的堆。
非托管堆是非托管代码/本机代码用于在运行时分配内存的堆。该堆不受垃圾收集器的控制,需要由开发人员处理以释放分配的内存。
“托管”和“非托管”之间的区别
In order to understand unmanaged Heap in .net we need to get an idea of what Managed Heap is.
In .net framework we have Garbage collector which is initialized by Common language routine.During its initialization Garbage collector allocates a segment of memory to store and manage objects instantiated by a Managed code .This memory is called the managed heap, as opposed to a native heap in the operating system.
Unmanaged heap is the one being used by unmanaged code/native code for allocating memory at run time.This heap is not under the control of garbage collector and it needs to be handled by the developer for freeing the memory allocated.
Difference between "managed" and "unmanaged"