How many heaps does the software have at least under windows

I found that C-runtime has its own heap (as well as the whole heap of APIs under HeapWalk windows ..). I have a bit of trouble with my previous knowledge of how it seems like a process has several heaps, not just one. It is right? And if so, then why is there a need for several heaps?

+4
source share
5 answers

A Windows process usually has at least 3 heaps:

  • heap of the default process. GlobalAlloc () extracts from it, mainly used by Windows
  • a bunch of COM. CoTaskMemAlloc () and SysAllocString () distinguish from it used by any COM server.
  • a bunch of CRT. The new operator and malloc () function are allocated from it.

Often there are several pieces of CRT, any DLL that was built with / MT has its own copy of CRT and thus gets its own heap. The exact reason she was not planning to ever single out from one heap is muddy for me.

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


UPDATE: the muddy part is a bit soft. Starting with VS2012, CRT now allocates the first pool from the default process heap. Keep in mind that this does not retroactively change the behavior of old DLLs that have not been restored.

+7
source

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

Why do you need more than one? Well, different heaps can be good at different things. Small objects could be well distinguished, one of which could be distinguished as large. It would be possible to detect heap allocation errors well, the other could be really fast, but it could cause your program to crash if used incorrectly. Etc etc.

+3
source

There is a concept of heaps in the Windows API, which is not very well known (judging by other answers so far). Honestly, 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 you are using, the behavior of the memory allocator on the heap can be slightly modified to accommodate different usage patterns (search low fragmentation heap). The most important difference is that memory obtained from one heap must be released into one heap. In most programs, you will not see or interact with different heaps directly, so you should not worry too much.

Just remember that when the library offers the Release method, you should use this and not delete , since the memory you got from the library may come from another heap.

+2
source

Processes have a heap area that can be expanded depending on how much space is required.

In fact, it happens that the stack grows, and the heap grows, you can expand the stack and the heap until there is no virtual memory left between them. The different heaps you see are likely different heap display ranges from memory mappings, etc.

+1
source

The heap propagates to higher memory addresses, while the stack grows toward lower addresses. The amount of heap available for the program depends on how many stacks are allocated. The stack contains a stack frame (local variables, parameters and registry pointers) of functions, and the heap is manually allocated using malloc. AFAIK, this is a situation for one program (i.e. a Process) without multiple threads.

0
source

All Articles