简体   繁体   English

关于堆和堆栈内存使用的问题

[英]Question about heap & stack memory usage

In windows operating system, the stack memory is a thread-specific storage, and the call stack is the logic flow of a series of methods. 在Windows操作系统中,堆栈内存是特定于线程的存储,而调用堆栈是一系列方法的逻辑流程。 So each thread has it's own stack area. 所以每个线程都有自己的堆栈区域。 I want to know how heap memroy area is used? 我想知道如何使用堆memroy区域? Is it thread-specific? 它是特定于线程的吗? Process-specific? 过程的具体情况? Or in .NET, AppDomian-specific? 或者在.NET中,特定于AppDomian? Or shared between all the user applications and the operating system? 或者在所有用户应用程序和操作系统之间共享? Many thanks. 非常感谢。

Heap is the most common way to implement dymanic memory allocation. 堆是实现动态内存分配的最常用方法。 The typical use scenario of using a heap includes when you do not know how much memory to allocate until runtime, or the desired memory is too large to be allocated in the stack. 使用堆的典型使用场景包括当您不知道在运行时分配多少内存,或者所需内存太大而无法在堆栈中分配时。

A process can hold one or more heaps. 一个进程可以容纳一个或多个堆。 Most processes have more than on heaps. 大多数流程都不仅仅是堆积。 For example in Windows a process can have default process heap, CRT heap, and the application can call Windows API to create its own heap (using API HeapCreate). 例如,在Windows中,进程可以具有默认进程堆,CRT堆,并且应用程序可以调用Windows API来创建自己的堆(使用API​​ HeapCreate)。

When a process is created the OS will create a new heap for it called Default Process Heap, which is actually rarely used in most cases. 创建进程时,操作系统将为其创建一个名为Default Process Heap的新堆,实际上在大多数情况下很少使用它。 When we call new/delete and malloc/free we are actually using the CRT heap. 当我们调用new / delete和malloc / free时,我们实际上正在使用CRT堆。

Windows uses some sophisticate datastructure and algorithm to ensure memory allocatioon/deallocation and mangement in heap is effective. Windows使用一些复杂的数据结构和算法来确保内存分配/释放和堆中的管理是有效的。 However the common fact is that allocating memory in heap can be much slower than in stack. 然而,常见的事实是在堆中分配内存可能比在堆栈中慢得多。

For more detailed information you can read Jeffrey Richter's great book Windows via C/C++ . 有关更多详细信息,您可以阅读Jeffrey Richter的伟大着作Windows via C / C ++ And you can read Here for some quick understanding of how heap is managed internally in Windows. 您可以阅读此处,以便快速了解如何在Windows内部管理堆。

Some background: a heap is typically used to hold memory that is dynamically allocated during program execution. 一些背景: 通常用于保存在程序执行期间动态分配的内存。 By contrast, memory on the stack is typically only used for the lifetime of a single function call - IE, when the function returns, the memory is no longer used. 相比之下, 堆栈上的内存通常仅用于单个函数调用的生命周期 - IE,当函数返回时,不再使用内存。

Each process has its own set of virtual memory, so different processes have their own private heaps. 每个进程都有自己的虚拟内存集,因此不同的进程都有自己的私有堆。

Threads within that process share the same pool of memory (heap), so care needs to be taken to make sure one thread does not "corrupt" memory of another. 该进程中的线程共享相同的内存池(堆),因此需要注意确保一个线程不会“破坏”另一个线程的内存。

Multiple AppDomains can run within a single process, but each one will have its own set of data and thus its own heap. 多个AppDomain可以在单个进程中运行,但每个进程都有自己的数据集,因此也有自己的堆。

"Multiple AppDomains can run within a single process, but each one will have its own set of data and thus its own heap." “多个AppDomain可以在一个进程中运行,但每个进程都有自己的数据集,因此也有自己的堆。”

Are you sure Justin, I have experimented with a simple command line application that loads another DLL into a separate application domain and executes it and then periodically report the memory usage. 你确定Justin,我已经尝试了一个简单的命令行应用程序,它将另一个DLL加载到一个单独的应用程序域并执行它,然后定期报告内存使用情况。

The code in the separate DLL is designed to gradually consume memory. 单独的DLL中的代码旨在逐渐消耗内存。

The result is that consol application reports the amount of allocated in its own application domain increasing until the whole lot is brought down by an out of memory exception on the thread running in the separate DLL under the separate application domain. 结果是consol应用程序报告在其自己的应用程序域中分配的数量增加,直到整个批次被单独的应用程序域下的单独DLL中运行的线程上的内存不足异常降低。

This behaviour was observed on a 32bit XP SP3 machine running .NET 2, and would sugest that separate AppDomains share the same Process heap. 在运行.NET 2的32位XP SP3计算机上观察到此行为,并且会使单独的AppDomain共享相同的进程堆。

If this not the experience of others, i would love to know how to insulate my applications from poisonous DLLs 如果这不是其他人的经验,我很想知道如何将我的应用程序与有毒的DLL隔离

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM