Linux memory management

Source: Internet
Author: User

Linux memory management

Most of the current servers run on Linux. Therefore, as a programmer, it is necessary to simply understand how the system runs. For the memory part, you need to know:

  1. Address ing
  2. Memory Management Method
  3. Page missing exception

First, let's look at some basic knowledge. In the process's view, the memory is divided into two parts: kernel state and user State. The classic ratio is as follows:

From the user State to the kernel state, it is generally implemented through system calls and interruptions. User-mode memory is divided into different regions for different purposes:

Of course, the kernel state will not be used differently, so the Division is as follows:

Next, let's take a closer look at how these memories are managed.

Address

In Linux, the address ing process is logical address> linear address> physical address. The physical address is the simplest: the digital signal transmitted in the address bus, the linear address and logical address represent a conversion rule. The linear address rules are as follows:

This part is completed by MMU, and the main registers involved include CR0 and S3. The logical address appears in the machine command. The logical address rules are as follows:

In Linux, the logical address is equal to the linear address. That is to say, Inter makes things complicated for compatibility, and Linux simplifies things by the way.

Memory Management Method

During system boot, the memory size and status will be detected. Before establishing a complex structure, you need to manage the memory in a simple way, which is bootmem, in simple terms, it is a bitmap, but there are also some optimization ideas.

The efficiency of bootmem optimization is not high. After all, we need to traverse the memory allocation. The buddy system can solve this problem: save some idle memory segments of power 2 internally. If you want to allocate 3 pages, retrieve one from the list of 4 pages, allocate 3 and then put the remaining 1 back, the memory release process is just a reverse process. It is represented by a graph:

We can see that 0, 4, 5, 6, and 7 are all in use. Will they be merged when 1 and 2 are released?

Staticinlineunsignedlong _ find_buddy_index (unsignedlong page_idx, unsignedint order) {return page_idx ^ (1 <order); // update the highest bit, 0 ~ 1 swap}

From the above code, we can see that 0 and 1 are buddy, and 2 and 3 are buddy. Although 1 and 2 are adjacent, they are not. Memory fragmentation is an enemy of system operation. The partner system mechanism can prevent fragmentation to a certain extent ~~ In addition, we can get the number of idle pages in each order through cat/proc/buddyinfo.

The memory allocated by the partner system is in the unit of page (4 kb), but the vast majority of data structures used during system operation are very small, it is clear that the allocation of 4 kb for a small object is not counted. In Linux, slab is used to allocate small objects:

During running, slab "wholesale" some memory to buddy and "sell" it after splitting. With the wide application of large-scale multi-processor systems and NUMA systems, slab has finally exposed the following shortcomings:

  1. Complex Queue Management
  2. Large overhead for managing data and queue Storage
  3. Long run of the partial queue may be very long
  4. The support for NUMA is very complex.

To solve these problems, the experts developed slub: modified the page Structure to reduce the slab management structure overhead, and each CPU has a local active slab (kmem_cache_cpu. For a small embedded system, there is an slab simulation layer slob, which is more advantageous.

The small memory problem is solved, but there is also a large memory problem: when the partner system allocates 10x4 kb of data, it will be searched in the Free List of 16x4 kb (the physical memory obtained in this way is continuous), but it is likely that there is memory in the system, but the partner system cannot allocate it, because they are split into small fragments. Then, vmalloc uses these fragments to piece together a large memory, which is equivalent to collecting some "Scraps" and assembling them into a finished product and then "selling ":

Previously, the memory was directly mapped. For the first time, we felt the existence of page Management: D. for high-end memory, we provided the kmap method to allocate a linear address to the page.

A process consists of segments of different lengths: code segments, dynamic library code, global variables, and stacks that dynamically generate data. In Linux, a virtual address space is managed for each process:

After writing the code malloc, we didn't immediately occupy that large physical memory. Instead, we only needed to maintain the above virtual address space and allocated physical memory only when needed, this is the COW (COPY-ON-WRITE: COPY-ON-WRITE) technology, and the physical allocation process is the most complicated process of page missing Exception Handling. Let's take a look at it!

Page missing exception

Before you actually need data in a virtual memory area, the ing relationship with the physical memory is not established. If the part of the virtual address space accessed by the process is not associated with the page frame, the processor automatically raises a page missing exception. The following information can be obtained when the kernel processes a page exception:

  1. Cr2: Linear address access
  2. Err_code: When an exception occurs, the control unit is pushed into the stack, indicating the cause of the exception.
  3. Regs: the value of the Register when an exception occurs.

The process is as follows:

In case of a page missing exception, swap may be sent to the disk because it is not often used. The swap related commands are as follows:

Command Function
Swapon Enable swap
Swapoff Disable swap
/Proc/sys/vm/swappiness The greater the score, the more active swap is. You can add vm. swappiness = xx to/etc/sysctl. conf to modify the value.

If the memory is mapped to the memory by mmap, a page error occurs during reading and writing of the corresponding memory.

This article permanently updates the link address:

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.