Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how many heaps have a software at least under windows

Tags:

c++

memory

I have discover that the C-runtime has in own heap ( and 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?

like image 906
Guillaume Paris Avatar asked Dec 10 '22 07:12

Guillaume Paris


1 Answers

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.

like image 77
Hans Passant Avatar answered Dec 26 '22 00:12

Hans Passant