Virtual memory (VirtualAlloc), Heap (heapalloc/malloc/new), and memory Mapped File

Source: Internet
Author: User

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

Very many people will be confused, but look at the following pictures will be more clear. This image is copied from MSDN.

Heap and virtual memory. As you can see from the image above, the so-called heap is abstracted out of virtual memory.

Assuming that the VIRTUALXXX series functions are used directly, there are some limitations, such as memory that can only be allocated a multiple of the page size at a time. The memory address must also be aligned. Beginners are very difficult to use. It is because of this that the heap appears. In fact, the inside of the heap is using the virtual series functions. The basic idea is to use VirtualAlloc to allocate a larger 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 starts at 100 bytes from the a address, and the second allocation is the 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, and on Windows, new calls the Malloc,malloc call heap function (HEAPXXX). The heap function calls the virtual series function. On Linux, the corresponding Linux API is called.

Virtual memory (VirtualAlloc), Heap (heapalloc/malloc/new), and memory Mapped File

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.