Malloc principle and memory fragmentation

Source: Internet
Author: User

When a process suffers a page disconnection, the process will be in the kernel state and perform the following operations::
1. Check whether the virtual address to be accessed is legal
2. Find/allocate a physical page
3. Fill in the physical page content (read the disk or directly set it to 0, or do nothing)
4,
Establish a ing relationship (from virtual address to physical address)
Re-execute the command with a page disconnection
If you need to read the disk in step 1, the page Disconnection will be majflt; otherwise, it will be minflt.

Principle of Memory Allocation

From the operating system perspective, there are two ways to allocate memory for processes:BRK and MMAP (do not consider shared memory ).

1. BRK pushes the highest address pointer _ edata of the Data Segment (. data) to the high address;

2. MMAP finds an idle virtual memory in the virtual address space of the process (in the middle of the heap and stack, called the place of the file ing area).

Both methods are allocated with virtual memory instead of physical memory..When you access the allocated virtual address space for the first time, a page disconnection occurs. The operating system allocates physical memory and establishes a ing between the virtual memory and physical memory.


In the standard C library, the malloc/free function is provided to allocate and release the memory. The two functions are called by BRK, MMAP, and munmap systems at the underlying layer.


The following example illustrates the principle of memory allocation:

Scenario 1: malloc memory less than kb. Use BRK to allocate memory and push _ edata to the high address (only virtual space is allocated and does not correspond to physical memory (so no Initialization is made ), when data is read/written for the first time, a page disconnection occurs in the kernel. The kernel allocates the corresponding physical memory and establishes a ing relationship between the virtual address space. For example:


 

1,The initial layout of the (virtual) memory space of a process during startup is shown in 1. Where, The MMAP memory ing file is in the middle of the heap and stack.(Such as libc-2.2.93.so, other data files, etc.), for the sake of simplicity, the memory ing file is omitted. _ Edata pointer (defined in glibc) points to the highest address of the Data Segment.
2,
After the process calls a = malloc (30 K), the memory space 2: the malloc function will call the BRK System Call and push the _ edata pointer to the high address for 30 K to complete the virtual memory allocation. You may ask:Just allocate _ edata + 30 K of memory? The fact is that _ edata + 30k only completes the allocation of virtual addresses. The memory of a still does not correspond to physical pages. When the process reads and writes the memory of a for the first time, when a page disconnection occurs, the kernel allocates a physical page corresponding to the memory. That is to say, if malloc is used to allocate the content of A and never access it, the physical page corresponding to a will not be allocated.
3,
After the process calls B = malloc (40 K), the memory space is 3.

 

Scenario 2: Use MMAP to allocate memory with malloc greater than kb, and find an idle memory allocated between the heap and stack (corresponding to the independent memory and initialized to 0), for example:

 

4,After the process calls C = malloc (200 K), the memory space is 4: by default, The malloc function allocates memory. If the request memory is greater than 128 KB (which can be adjusted by the m_mmap_threshold option), it does not push _ edata pointer, but is called by the MMAP system, allocate a virtual memory from the middle of the heap and stack. This is mainly because :: The memory allocated by BRK can be released only after the High-address memory is released. (For example, before B is released, a cannot be released. This is why memory fragments are generated, the following figure shows when the compression occurs. The memory allocated by MMAP can be released separately. Of course, there are other advantages and disadvantages. If you are interested, you can check the malloc code in glibc.
5,After the process calls d = malloc (100 K), the memory space is 5;
6,After the process calls free (c), the virtual memory corresponding to C is released together with the physical memory.

 

 

7,7 is shown after the process calls free (B: The virtual memory and physical memory corresponding to B are not released, because there is only one _ edata pointer. If we push back, what should we do with d memory?? Of course, the memory of B can be reused. If another 40 k request is made at this time, malloc may return the memory of B..
8,After the process calls free (d), it is shown in 8: B and D are connected to form a K idle memory. 9,By default: When the free memory of the maximum address space exceeds 128 K (which can be adjusted by the m_trim_threshold option), the memory compression operation (TRIM) is executed ).When the last step is free, the maximum address idle memory exceeds 128 kb, so the memory is reduced, as shown in figure 9.

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.