开发者

how many heaps have a software at least under windows

开发者 https://www.devze.com 2023-03-18 23:38 出处:网络
I have discover that the C-runtime has in own heap ( and 开发者_JAVA技巧also all the Heap API under windows HeapWalk..).I\'m a little bit in trouble in regard of my previous knowledge in the way it se

I have discover that the C-runtime has in own heap ( and 开发者_JAVA技巧also all the Heap API under windows HeapWalk..). I'm a little bit in trouble in regard of my previous knowledge in the way it seems a Process have in fact several Heaps and not only one. Is it right ? And if it is the case, why there is the need of several Heaps?


A Windows process generally has at least 3 heaps:

  • the default process heap. GlobalAlloc() allocates from it, mostly used by Windows
  • the COM heap. CoTaskMemAlloc() and SysAllocString() allocate from it, used by any COM server
  • the CRT heap. The new operator and malloc() function allocate from it.

It is not uncommon to have multiple CRT heaps, any DLL that was built with /MT has its own copy of the CRT and thus gets its own heap. The exact reason why it wasn't contemplated to only ever allocate from a single heap is murky to me.

The GetProcessHeaps() function is available to iterate all the heaps in the process.


UPDATE: the murky part got unmurked a bit. Starting with VS2012, the CRT now allocates from the default process heap, first bullet. Keep in mind that this doesn't retroactively changes the behavior on old DLLs that were not rebuilt.


A heap is just a data structure. You can have as many as you want, from the C runtime, from the O/S, from a third party, or you can write your own.

Why would you need more than one? Well different heaps might be good at different things. One might be good at allocating small objects, one good at allocating large. One might be good at detecting heap allocation errors, another might be really fast, but liable to crash your program if misused. Etc, etc.


There is a concept of heaps in the windows API that is not too widely known (judging by the other answers up to now). To be honest I don't know much either, but here it is:

The windows API allows you to create different heaps which means that the total number will depend on the application that you are running, the behavior of the memory allocator in the heap can be slightly changed to accommodate for different usage patterns (search for low fragmentation heap). The most important difference is that memory acquired from one heap must be released to the same heap. In most programs you will not see or interact with the different heaps directly, so you should not worry too much.

Just remember that when a library offers a Release method, you should use that and not delete, as the memory that you obtained from the library might come from a different heap.


Processes have a Heap area that can be extended depending on how much space is needed.

What actually happens is that the stack grows up and heap grows down, you can extend the stack and heap as long as there is virtual memory left in between. The different heaps you're seeing is probably the different mapped heap ranges from memory mappings and such.


The heap extends towards higher memory addresses while the stack grow up towards lower addresses. The amount of heap available for a program depends on how much stack is allocated. The stack contains the stack-frame (local variables, parameters and registry pointers) of the functions, while the heap is manually allocated with malloc. AFAIK, this is the situation for one single program (i.e. process) without multiple threads.

0

精彩评论

暂无评论...
验证码 换一张
取 消