There are three ways in which memory management works:
1. Virtual memory, functions such as VirtualAlloc
2. Heap, heapxxx function, malloc,new, etc.
3. Memory-mapped files, Mapped file
Many people will be confused, but look at the picture below will be more clear. This image is copied from MSDN.
Heap and virtual memory, from the above image can be seen, in fact, the so-called heap, that is, virtual memory abstraction. If you are using the VIRTUALXXX series function directly, there are some limitations, such as memory that can only be allocated a multiple of the page size at a time, and the memory address must be aligned. Beginners are hard to use. Because of this, the heap appeared. In fact, the inside of the heap is using the virtual series functions. The basic idea is to use VirtualAlloc to allocate a relatively large amount of memory, and then each time the user requests the heap memory, from the allocated virtual memory block to specify a piece to the user. For example, allocating heap memory for the first time is 100 bytes from the a address, and the second allocation is a+100 start.
Memory Mapped file is not the same, from the above figure can be seen that MMF did not call the virtual series function. It calls the kernel layer directly.
For malloc and new, these 2 are not operating system APIs, they are language-provided functions. There are different implementations on different systems, on Windows, new calls the Malloc,malloc call heap function (HEAPXXX), and the heap function calls the virtual series function. On Linux, the appropriate Linux API is called.
Virtual memory (VirtualAlloc), Heap (heapalloc/malloc/new), and memory Mapped File