非托管内存和托管内存
非托管
和托管内存
到底是什么? 有人可以简单地解释一下我吗?
另外,当托管内存概念应用于 RAM(称为托管 RAM)时,到底意味着什么。 “托管 RAM”和“非托管 RAM”有哪些具体细节?
what exactly are un-managed
and managed memory
?
can anybody explain me in brief?
Also, what exactly would mean when the managed-memory concept is taken to RAM, calling managed-RAM. What are some of the specifics about "managed RAM" and "un-managed-RAM"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
都是相同的物理内存。区别在于谁在控制它。
Microsoft 的定义是,托管内存由垃圾收集器 (GC) 进行清理,即定期确定物理内存的哪些部分正在使用、哪些部分未使用的进程。
非托管内存由其他东西(例如您的程序或操作系统)清理。
非托管内存这个术语有点像第一次世界大战,直到第二次世界大战之后才被称为非托管内存。以前它只是内存。
It is all the same physical memory. The difference is who is controlling it.
The Microsoft definition is that managed memory is cleaned up by a Garbage Collector (GC), i.e. some process that periodically determines what part of the physical memory is in use and what is not.
Unmanaged memory is cleaned up by something else e.g. your program or the operating system.
The term unmanaged memory is a bit like the World War 1, it wasn't called that until after World War 2. Previously it was just memory.
可以从不同的方面来理解
非托管内存
。在较高级别上,您运行的应用程序可以分为两类:
非托管代码
和托管代码
。托管代码
由运行时
管理,例如.Net和Java。运行时提供了重要的功能,例如自动内存管理(通过垃圾收集器)。非托管代码
是运行 ac/c++ 程序的方式。开发人员全权负责管理程序的所有内容,包括内存部分(通过malloc
和free
等 API)。在较低且具体的层面上,例如在.Net世界中,内存管理也可以分为
托管
和非托管
两部分。这其实是令人困惑的一点。因为如上所述,.Net运行时提供了GC,可以自动处理内存分配和释放。为什么仍然有非托管
内存?这里的关键点是,非托管部分不是内存本身,而是绑定到内存的底层资源。一般来说,这些是操作系统级别的资源,如文件
、网络连接
和数据库连接
。 GC 可以收集内存,但它不知道如何处理这些底层系统级资源。所以在某种程度上,它不受运行时的控制。然后开发人员需要做一些事情来帮助运行时正确处理这种情况(例如.Net世界中的Dispose
方法调用)。There are different aspects to understand
unmanaged memory
.In the higher level, the applications you run can be classified into two categories:
unmanaged code
andmanaged code
. Themanaged code
is managed by aruntime
, like .Net and Java. The runtime provides important functionalities like automatic memory management(by garbage collector). And theunmanaged code
is the way you run a c/c++ program. The developers have the full responsibility to manage everything of the program, including the memory part(by APIs likemalloc
andfree
).In a lower and specific level, for example, in .Net world, the memory management can also be classified into
managed
andunmanaged
two parts. This is in fact the confusing point. Because as mentioned above, the runtime of .Net provide GC which can handle the memory allocation and release automatically. Why there are stillunmanaged
memory? The critical point here is that theunmanaged
part is not the memory itself, instead it's the underlying resources bound to the memory. Generally, these are operating system level resource, likefile
,network connection
anddatabase connection
. The GC can collect the memory but it doesn't know how to handle those underlying system level resource. So to some extend, it's out of control of the runtime. Then the developer need to do something to help the runtime to handle this case properly(For example, theDispose
method call in .Net world).