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. So each thread has it's own stack area. I want to know how heap memroy area is used? Is it thread-specific? Process-specific? Or in .NET, AppDomian-specific? Or shared between all the user applications and the operating system? Many thanks.
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.
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.
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).
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. When we call new/delete and malloc/free we are actually using the CRT heap.
Windows uses some sophisticate datastructure and algorithm to ensure memory allocatioon/deallocation and mangement in heap is effective. 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++. And you can read Here for some quick understanding of how heap is managed internally in Windows.