Linux Memory Management

Source: Internet
Author: User

A brief introduction to the basic concepts of memory management and the allocation of memory mechanisms on Linux.

1 Basic Concepts

1.1 Address

*) Logical address: Refers to the part of the offset address generated by the program associated with the segment. In the C language pointer, read the pointer variable itself value (& operation), which is actually the logical address, which is relative to your current process data segment address. The base address of the data segment is stored in the Global Descriptor Table/local Descriptor list.

*) Linear Address: The offset address in the segment, plus the base address of the corresponding segment, generates a linear address. is an intermediate value.

*) Physical Address: Addressing the address on the bus.

*) virtual Address: Protected mode The address of the offset within the segment and segment. The logical address is the offset within the code snippet, or the logical address of the process.

1.2 Memory

*) virtual Memory: a technology of computer system memory management. It makes the application think it has contiguous free memory, and in fact, he is often separated into multiple physical memory fragments, and some are temporarily stored on the external disk's storage, and the data is exchanged when needed. Systems that use this technology make it easier to write large programs than systems that do not use virtual memory technology, and are more efficient at using real physical memory.

*) Physical Memory: actual memory. The physical address is divided into discrete units that become pages (page). Most systems currently have a page size of 4k.

1.3 Address Translation

Linux uses a section-page management mechanism with two parts for address translation: segmented parts and pagination parts.

*) Segmented part: Converts the logical address into a linear address. Fragmentation provides a mechanism for isolating individual code, data, and stack areas. So multiple programs can run on a single processor at the same time without interfering with each other.

*) Paging parts: Convert the linear address to Physical address (page table and page directory), without paging mechanism, then the linear address is the physical address.

2 Memory allocation

The difference between Malloc/kmalloc/vmalloc:

*) Kmalloc and Vmalloc allocate memory for kernel space. malloc allocates memory for user space.

*) Kmalloc guarantees that the distribution is physically contiguous, and Vmalloc is guaranteed to be contiguous on the virtual address space.

*) Kmalloc application memory is relatively small, generally less than 128K, it is based on slab (memory pool), to speed up the application of small memory efficiency.

3 FAQs

3.1 Will the OS allocate physical memory immediately after calling the malloc function?

When using malloc to request memory, the function only returns a virtual address, and when the process is going to use memory, the OS issues a fault with the fault, and the memory management module allocates real memory for the process.

Advantages and disadvantages of 3.2-segment and page-managed management

In the segment storage management, the address space of the program is divided into several segments (segment), so that each process has a two-dimensional address space, independent of each other, non-interference. The program is divided into several modules, such as code snippets, data segments, shared segments, and stack segments. The advantage of this is that you can write and compile one file for the source program separately, and you can take different protections for different types of segments, or you can share them as broken units. The advantage of segment storage is that there is no inner fragment, and outer fragments can be eliminated by memory crunch, which facilitates memory sharing.

In page storage management, the logical address space for a program is divided into fixed-size pages, and physical memory is also divided into a fixed-size page box. When the program loads, you can put any page into any of the in-memory page boxes, which do not have to be contiguous, allowing for discrete allocations. The advantage of this management method is that there is no outer fragment, and a program does not have to be kept in succession, which makes it easy to change the size of the program footprint.

There are many similarities between page-and segment-style systems. For example, both adopt a discrete allocation method, and all through the address mapping mechanism to achieve address transformation. But there are many different concepts, mainly in:

*) page is the physical unit of information, paging is to achieve discrete distribution, to reduce the amount of external memory, improve memory utilization. Or, paging is simply due to the need for system management, not the needs of the user. A segment is a logical unit of information that contains a set of information that is relatively complete in its meaning. The purpose of segmentation is to better meet the needs of users.

*) The size of the page is fixed and has a system decision, the logical address is divided into a good and the page address two parts, is implemented by the machine hardware. The length of the segment is not fixed and is determined by the user-written program, usually by the compilation system when the source program is compiled according to the nature of the information to be divided.

*) page system address space is one-dimensional, that is, a single linear address space. A programmer can represent an address simply by using an identifier. The job address space of the segment is two-dimensional, when the programmer identifies an address, it needs to give both the segment name and the address in the paragraph.

3.3 malloc What is the case when calling Mmap?

From the operating system's point of view, the process allocates memory in two ways, which are done by two system calls: Brk/mmap (regardless of shared memory). BRK is to push the highest address pointer of the data segment (. data) _edata toward the high address, mmap is to find an idle piece in the virtual address space of the process (usually the middle of the queue and stack). Both of these allocations are allocated virtual memory and no physical memory is allocated. In the first access to the allocated virtual address space, a page break occurs, the operating system is responsible for allocating physical memory, and then establish a mapping between virtual and physical memory.

In the standard C library, Malloc/free functions are provided to allocate and free memory, which is implemented by MMAP/BRK and Munmap these system calls. By default, the malloc function allocates memory, and if the request memory is greater than 128K (which can be adjusted by the M_mmap_threshold option), instead of pushing the _edata pointer, the MMAP system call is used to allocate a piece of virtual memory from the middle of the heap and stack. This is done primarily because the memory allocated by the BRK needs to wait until the high address memory is freed, and the mmap allocated memory can be freed separately.

3.4 32-bit systems, typically, what is the maximum virtual address and physical address space?

In the case of PAE, the maximum virtual address and physical address space are 4G, and if PAE is used, the maximum virtual address space is still 4G, and the physical address space can be changed to 64G (x86,32 bit to 36 bits).

4 Illustration of memory allocation principle

*) When the process starts, the initial layout of its memory space is shown in 1. Where the mmap memory-mapped file is in the middle of the heap and stack. For simplicity, the memory-mapped file is omitted. The _edata pointer (defined inside the glibc) points to the highest address of the data segment.

*) After the process calls a (malloc (30K)), the memory space is 2. The malloc function invokes the BRK system call and pushes the _edata pointer toward the high address by 30K, completing the virtual address assignment. Here, just push the _edata pointer high 30k,a the memory is still not corresponding to the physical page. 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 you use malloc to assign a, but never access a, the corresponding physical page of a is not assigned.

*) After the process calls B (malloc (40K)), memory space 3.

*) After the process calls C (malloc (200K)), the memory space is 4. By default, the malloc function allocates memory, and if the requested memory space is greater than 128K, it does not push the _edata pointer, but instead uses the MMAP system call to allocate a piece of virtual memory from the middle of the heap and stack.

*) After the process calls D (malloc (100K)), the memory space is 5.

*) After the process calls free (c), C corresponds to the virtual memory and physical memory released together. 6.

*) process calls free (B), as shown in 7. The virtual memory and physical memory for B are not released because there is only one _edata pointer. If you push back, then D is not good. b This piece of memory can be reused. If this is a 40K request, then malloc is likely to return the B memory back.

*) After the process calls free (D), if 8 is shown. B and D are joined together and become a piece of 140K of free memory.

*) By default, when the maximum amount of free memory in the 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, it was found that the highest address was idle by more than 128K, so the memory crunch was shown in 9.

5 cases

5.1 Problem phenomena

During the stress test process, the system State CPU consumption of the process occurs 20, the user state CPU consumes 10, the system idle is about 70, and the Ps-o majflt,minflt-c Program command is used to view the Majflt increments of 0 per second, and minflt increments of more than 10000 per second.

5.2 Problem Analysis

Knowing the principle of memory allocation, the module in the kernel State CPU consumption is very high reason is clear. In the code, each request comes with malloc a piece of 2M of memory, and by default, malloc calls Mmap (greater than 128K) to allocate memory. At the end of the request, call Munmap to free memory. Assuming that each request requires 6 physical pages, each request generates 6 page faults, and at 2000, it generates more than 10,000 minflt per second, which does not require reading the disk. The fault is executed in the kernel state, so the process in the kernel CPU consumption is very large, the fault is scattered throughout the processing of the request, so the expression is the allocation statement time (10US), and the entire request processing time is 1000US, the proportion is very small.

5.3 Solutions

Changes dynamically allocated memory to statically allocated memory. Or, at boot time, use malloc to assign each thread, and then save it in Threaddata. However, because of the specifics of the module (which is unclear), it is not possible to use static allocations or assign to each thread. In addition, the default stack size under Linux is limited to 10M, which is risky if you allocate a few m of memory on the stack.

Here's how to fix it:

Disabling malloc calls Mmap allocating memory, preventing memory crunch.

When the process starts, add the following two lines of code:

Mallopt (M_mmap_max, 0); Disable malloc calls Mmap allocate memory

Mallopt (M_trim_threshold,-1); Disabling memory Crunch

After adding these two lines of code, the PS command is used to observe that the Majflt and Minflt increments are 0 after the pressure is stabilized.

Linux Memory Management

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.