13. Windows API memory management (3)

Source: Internet
Author: User

Ii. Heap Management

The memory allocated by the user's memory allocation function is in the heap. Therefore, using the heap management function to allocate and release the memory is the most direct method.

In Windows, each process has its own heap, and the number of heap processes varies.In Windows, the so-called heap is not a memory block, but an object used for memory management and a memory organization.Processes can allocate and release memory on their own stacks. The heap includes several attributes, for example, the maximum heap size can be fixed or "scalable". Can the data content on the heap be usedCodeExecutable; whether the heap can be connected and accessed.

Iii. Global and local memory management

In 32-bit Windows systems, global and local functions are not distinguished. In Windows systems, the concept of global and local functions is to be compatible with 16-bit systems. There is no difference in the memory allocated by the globalalloc and localalloc functions. The effect of memory allocation is similar to that of the heapalloc function. However, memory allocation between globalalloc and localalloc is slower than heapalloc.

Therefore, we recommend using the heap function.

Iv. Virtual Memory Management

The principle of memory management is implemented by the Memory Manager of the system.ProgramYou can only manage attributes in your own virtual address space.

The virtual address space memory page of a process has three statuses: free, reserved, and committed ).

5. Comparison of Memory Allocation

Using functions such as virtualalloc and virtualfree, the basic unit of operations is the Memory Page.

Heapalloc, globalalloc, and loaclalloc have no major functional differences. They all allocate memory in the heap, and the allocated memory does not require page alignment, you do not need to care about the paging mechanism, Page Status, page attributes, and other content. Heapalloc is the most direct way for programs to allocate memory. The heap manager is responsible for the content.

Heap memory management depends on virtual memory management. When creating a heap, The heapcreate API function will request the virtual memory pagination from the system, the memory allocation on the heap is actually allocating any memory block in size on the pages of the memory obtained from the virtual memory management.If a fixed heap size is specified During Heap creation, the range of memory blocks allocated to the heap cannot exceed the configured heap size, the heap size must be an integer multiple of the memory page size. If the heap size is not fixed when the heap is created, the heap management function dynamically requests the memory pagination to the virtual content management function based on the number of allocated requests.

Virtualalloc is used to manage the memory paging status in the virtual address space of a process.Virtual Memory Management (in Memory Page). Of course, the memory allocation is also achieved by changing the page attribute of the virtual memory, so virtualalloc indirectly achieves the purpose of allocating memory. The heap Manager also relies on virtual memory management. When receiving heapalloc memory allocation requests, the heap Manager determines whether to use the virtual memory management mechanism to allocate new pages based on the situation, and how to layout allocated memory blocks on the page,At this level, virtualalloc is more underlying..

In fact, as long as the virtual memory management function sets the page attribute to "submitted", you can perform read/write operations on the top.

Heap management functions provide applications with a more flexible and simple way to manage these committed memories. The advantage is that the allocation is simpler, the other is that the memory of any size can be allocated, and the third is that it does not have to deal with the replication memory paging mechanism directly.

The memory allocation function of the Standard C function directly uses heapalloc.

Heapalloc, globalalloc, and loaclalloc have the same functions on 32-bit systems. However, because Globa] _ alloc and loaclalloc must be compatible with 16-bit systems, more judgment and processing are required, therefore, globalalloc and loaclalloc are less efficient than heapalloc. Of course, the standard C function malloc is also implemented by calling heapalloc, so its efficiency must be lower than heapalloc.

Because heapalloc and virtualalloc functions are different in positioning, there is no comparable efficiency. In principle, when heapalloc allocates memory, if the heapalloc has enough submitted pages for use, it does not need to change the memory pages from other States to submitted states, you only need to perform relevant management in the heap manager. If there are not enough submitted pages for use during allocation, you also need to change the virtual memory page from other States to submitted states, if there are enough available submitted pages, the heap manager will perform relevant management.

Related Article

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.