"Go" The lower level implementation of malloc and free

Source: Internet
Author: User

This article transferred from: Http://blog.163.com/[email protected]/blog/static/132229655201210975312473/

How do I see how many times a process has a missing pages break ?

View with the Ps-o majflt,minflt-c program command.

Majflt representative Major fault, Chinese name big mistake , Minflt representative minor fault, Chinese name is small error .

These two values represent the number of fault pages that have occurred since the start of a process.

After a missing pages break, what are the actions performed?

when a process occurs with a missing fault, the process falls into the kernel state and performs the following actions :
1. Check if the virtual address you want to access is legitimate
2.
Find/Assign a physical page
3, fill the physical page content (read the disk, or directly set 0, or
do nothing)
4. Establish a mapping relationship (virtual address to physical address)
Re-execute the command that occurred with a missing pages interrupt
If the 3rd step, need to read the disk, then this time the fault is Majflt, otherwise it is minflt.

The principle of memory allocation

From the operating system perspective, the process allocates memory in two ways, with two system invocations:BRK and mmap (regardless of shared memory).

1, BRK is the data segment (. data) The highest address pointer _edata to the high address of the push;

2, Mmap is in the virtual address space of the process (heap and the middle of the stack, called the file map area ) to find a piece of free virtual memory .

Both of these methods allocate virtual memory and no physical memory is allocated . A page break occurs when the allocated virtual address space is accessed for the first time, the operating system is responsible for allocating physical memory, and then establishing a mapping relationship between virtual memory and physical memory.


In the standard C library, Malloc/free function allocations are provided to release memory, which is implemented by brk,mmap,munmap these system calls.


Here's an example of how memory allocation works:

scenario One,malloc less than 128k of memory, using BRK to allocate memory, the _edata to high address push (only the virtual space, not the corresponding physical memory (hence no initialization), the first read/write data, causing the kernel page fault, the kernel allocates the corresponding physical memory, The virtual address space is then mapped to a mapping relationship), such as:

1 . When the process starts, the initial layout of its (virtual) memory space is shown in 1. where the mmap memory map file is in the middle of the heap and stack (for example, libc-2.2.93.so, other data files, etc.), for the sake of simplicity,the memory-mapped file is omitted. The _edata pointer (defined inside the glibc) points to the highest address of the data segment.
2 . After the process calls A=malloc (30K), Memory space 2:The malloc function invokes the BRK system call and pushes the _edata pointer toward the high address by 30K, completing the virtual memory allocation. you may be asking:Just put the _edata+30k on the memory allocation? The truth is, _edata+30k just completes the assignment of the virtual address,A This memory is still no physical page corresponding to it,When the process first reads and writes a memory, a page break occurs, at which point the kernel allocates the physical pages of the memory corresponding to a. That is, if malloc assigns a block of content and then never accesses it, a corresponding physical page is not assigned.
3,
The process calls B=malloc (40K) after the memory space 3.

In case two,malloc is larger than 128k of memory, use MMAP to allocate memory, find a free memory allocation between heap and stack (corresponding to independent memory, and initialize to 0), such as:

4 . After the process calls C=malloc (200K), Memory space 4:by default, the malloc function allocates memory, if the request memory is greater than 128K (which can be adjusted by the M_mmap_threshold option), it is not to push the _edata pointer, but to use the MMAP system call, Allocate a piece of virtual memory from the middle of the heap and stack . This is done mainly because : the memory allocated by the BRK needs to wait until the high address memory is freed (for example, before B is released, A is not possible to release, which is why memory fragmentation occurs, and when to tighten to see below), The memory allocated by MMAP can be freed separately. of course, there are other benefits, there are disadvantages, and then specific, interested students can go to see the glibc inside the malloc code.
5, the process calls D=malloc (100K) after the memory space 5;
6, after the process calls free (c), C corresponds to the virtual memory and physical memory released together. 7.After the process calls free (B), 7 shows: b corresponds to the virtual memory and physical memory are not released, because there is only one _edata pointer, if push back, then d this memory what to do?Of course, b this memory can be reused, if a 40K request at this time, then malloc will probably put b this memory back
8.After the process calls free (D), 8 shows: B and D are joined together and become a piece of 140K of free memory. 9.By default: When the free memory of the highest address space exceeds 128K (which can be adjusted by the M_trim_threshold option), the memory crunch operation (TRIM) is performed. In the previous step free, the highest address was found to have more than 128K of memory, and the memory crunch became shown in Figure 9.

"Go" The lower level implementation of malloc and free

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.