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

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

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.