Two concepts about heap on Windows after reading MSDN document about heap functions.

1. Default heap. Each process has a default heap. But the default heap of different processes are different, right? Example, process 1 has default heap A and process 2 has default heap B, then A and B should be different heaps, right?

2. Why a process needs to allocate private heap, any practical use?

3. Are there any default global heap which different processes could share?

thanks in advance,
George

11-10-2007

matsp

1. Yes, different applications have their own heaps. They are "personal" not "communal".

2. I guess if you are writing something like a Java VM or a Lisp interpreter, you may want to have one heap that the VM uses, and one heap that the VM/interpreter uses for it's own internal purposes [that is, if you have for example a symbol table in the interpreter, that comes from the "interpreter heap", but a list of Lisp objects that belong to the lisp-code itself is allocated from the "lisp heap"]. There may be other situations too where an application could use multiple heaps - the reason generally would be to "split things up so that the different big blocks of functions don't interfere with each other". I expect you could have a separate heap for the GUI and "document" in a Word Processor for example.

3. No, that would be shared memory, and you could of course build your own heap in some shared memory region, but the OS doesn't support this as such - and it would be much harder to manage, because you need to use locking to prevent two apps from "racing" each other when it comes to allocations, freeing and such.

--
Mats

11-10-2007

abachler

Quote:

Originally Posted by George2

2. Why a process needs to allocate private heap, any practical use?

Because there is a 2GB (3GB*) per process limit, but many motherboards support up to 4GB total system memory. By gving each process its own heap, windows can move the application around in memory and just change its segment registers without breaking the application. If multiple applications used the same heap, windows couldnt move the heap around, adn since a procvess can execute code on the heap, it woudl limit all processes to sharing the same 2GB and it woudl prevent windows from moving applications aroudn to prevent fragmentign the memory. So If I ran an aplication that uses 1GB adn then another that runs 1GB adn then one mor that runs 1GB then I shut the second one down, with a common heap I could not run a 2GB process because there woudlnt be a contiguous 2GB block of memory available. With per process heaps however, I can move teh 3rd program down simply by reaassigning its segment regeiers and fixing up the paging table. Remember, yoru program goes on the heap, global variables and allocated memory comes from the heap, local variables are created on the stack.