Linux Memory management

Source: Internet
Author: User
Some time ago, I read "deep understanding of Linux kernel" and spent a lot of time on memory management. However, there are still many problems that are not very clear. Recently I spent some time reviewing them, here, I will record my understanding and my views on Memory Management in Linux.

Some time ago, I read "deep understanding of Linux kernel" and spent a lot of time on memory management. However, there are still many problems that are not very clear. Recently I spent some time reviewing them, here, I will record my understanding and my views on Memory Management in Linux.

I like to figure out the development history of a technology. In short, it is about how the technology develops. what technologies existed before this technology and what features these technologies have, why is it replaced by the current technology, and the current technology solves the problems existing in the previous technology. After understanding this, we can grasp a technology clearly. Some materials directly introduce the meaning and principles of a concept, but do not mention the development process and the principles behind it, as if the technology fell from the sky. In this regard, we will continue to talk about the theme of today with the development history of memory management.

First of all, I must elaborate on the segmentation and paging technologies in Linux memory management.

Let's review the history. in early computers, programs run directly on the physical memory. In other words, a program accesses a physical address during running. If this system runs only one program, as long as the memory required by this program does not exceed the physical memory of the machine, there will be no problems, and we do not need to consider the troubles of memory management, it's just a program. it's just a little memory. if you don't eat enough, that's your business. However, the current system supports multi-task and multi-process processing, so that the CPU and other hardware utilization will be higher, at this time, we need to consider how to allocate the limited physical memory in the system to multiple programs in a timely and effective manner. this is called memory management.

The following is an example of memory allocation management in an early computer system for your understanding.

We have three programs, Program 1, 2, and 3. in the process of Program 1 running, 10 MB of memory is required; in the process of program 2 running, MB of memory is required; in the process of program 3 running, 20 MB of memory is required. If the system needs to run programs A and B at the same time, the early memory management process is like this. allocate the first 10 MB of physical memory to A and the next 10 m-110m to B. This kind of memory management method is direct and good. let's assume that we want Program C to run at this time, and assume that our system has only 128 MB of memory, obviously, Program C cannot run due to insufficient memory. We know that we can use the virtual memory technology. when there is not enough memory space, we can swap the data that is not needed by the program to the disk space, which has reached the purpose of expanding the memory space. Next, let's take a look at some obvious problems in this memory management method. As mentioned at the beginning of the article, it is best to clearly understand the development process of a certain technology.

1. process address space cannot be isolated

Because the program directly accesses the physical memory, the memory space used by the program is not isolated. For example, as mentioned above, the address space of A is in the range of 0 to 10 m, but if A has A piece of code that operates data in the address space of 10 M to M, then program B and program C are likely to crash (each program can have the entire address space of the system ). In this way, many malicious programs or Trojan programs can easily break other programs, and the security of the system is not guaranteed, which is intolerable for users.

2. low memory usage efficiency

As mentioned above, if we want to run programs A, B, and C at the same time, the only way is to use the virtual memory technology to write data temporarily not used by some programs to the disk, read back the memory from the disk as needed. Here, Program C is running, and switching A to the disk is obviously not good, because the program requires A continuous address space, and program C requires 20 MB of memory, while A only has 10 MB of space, so it is necessary to switch Program B to the disk, and B has 100 MB of space, we can see that in order to run program C, we need to write MB of data from the memory to the disk, and then read the memory from the disk when program B needs to run. we know that IO operations are time-consuming, therefore, the efficiency of this process will be very low.

3. the program running address cannot be determined.

Each time the program needs to run, it must be out of memory with a large enough idle area, and the problem is that the idle location is uncertain, which will lead to some relocation problems, the problem of relocation is determined by the variables and function addresses referenced in the program. if you do not understand it, you can check the compilation willingness information.

Memory management is nothing more than trying to solve the above three problems, how to isolate the address space of the process, how to improve the memory usage efficiency, and how to solve the problem of relocation during program running?

Here, we reference an uncertain saying in the computer field: "Any problems in computer systems can be solved by introducing an intermediate layer ."

The current memory management method is to introduce the virtual memory concept between the program and the physical memory. The virtual memory is located between the program and the internal memory. The program can only see the virtual memory and can no longer directly access the physical memory. Each program has its own process address space, thus achieving process isolation. Here, the process address space refers to a virtual address. Since it is a virtual address, it is also a virtual address, and it is not a real address space.

Since we have added a virtual address between the program and the physical address space, we need to solve how to map the virtual address to the physical address, because the program will eventually run in the physical memory, there are two main technologies: segmentation and paging.

Segment (Segmentation): This method is a method that people first use. The basic idea is to map the virtual space of the memory address space required by the program to a physical address space.

Field ing mechanism

Each program has its own virtual independent process address space. we can see that the virtual address space of program A and program B starts from 0x00000000. We map two virtual address spaces of the same size to the actual physical address space, that is, each byte in the virtual address space corresponds to each byte in the actual address space, in this ing process, the software sets the ing mechanism, and the actual conversion is completed by the hardware.

This segmented mechanism solves the issue of process address space isolation and program address relocation in the three problems mentioned at the beginning. Programs A and B have their own virtual address space, and the virtual address space is mapped to A physical address space that does not overlap with each other, if the IP address used by program A to access the virtual address space is not in the range 0x00000000-0x00A00000, the kernel rejects the request, so it solves the problem of isolating the address space. Our application A only needs to care about its virtual address space 0x00000000-0x00A00000, and we do not need to care about which physical address it is mapped to. Therefore, the program will always place variables and code according to the virtual address space, you do not need to relocate it.

No matter how the segmentation mechanism solves the above two problems, it is a great improvement, but there is still nothing to do with memory efficiency. Because this memory ing mechanism is still based on programs, when the memory is insufficient, the entire program needs to be switched to the disk, so the memory usage efficiency is still very low. So, how can we calculate high-efficiency memory usage. In fact, according to the local running principle of a program, only a small amount of data is frequently used during the running process of a program within a certain period of time. So we need a smaller granularity of memory segmentation and ing methods. will we think of the Buddy algorithm in Linux and the slab memory allocation mechanism? haha. Another method for converting a virtual address to a physical address is the paging mechanism.

Paging mechanism:

The paging mechanism divides the memory address space into several small and fixed pages. The size of each page is determined by the memory, just like the ext file system in Linux divides a disk into several blocks, this is done to improve the memory and disk utilization. Imagine that if the disk space is divided into N equal parts, the size of each part (a Block) is 1 MB. if I want to store the file on the disk to 1 KB, then the remaining 999 bytes are not wasted. Therefore, we need a more fine-grained disk partitioning method. we can set the Block to a smaller value. of course, this is based on the size of the stored files. it seems a bit out of question, I just want to say that the paging mechanism in the memory is very similar to the disk partitioning mechanism in the ext file system.

In Linux, the general page size is 4 kB. we split the process address space by page, and load common data and code pages to the memory, the code and data that are not commonly used are stored on the disk. we will illustrate it with an example, for example:

Page ing between process virtual address space, physical address space, and disk

We can see that the virtual address space of Process 1 and process 2 are mapped to a non-sequential physical address space (this makes a lot of sense. if one day we do not have enough physical address space, but there are a lot of discontinuous address spaces. without this technology, our program won't be able to run.) even if they share part of the physical address space, this is shared memory.

The virtual pages of Process 1, CIDR block, and VP3, are switched to the disk. when the program needs these two pages, the Linux kernel will generate a missing page exception, then the exception manager reads it into the memory.

This is the principle of the paging mechanism. of course, the implementation of the paging mechanism in Linux is still complicated. the implementation is also a global directory, a parent directory, and a page intermediate directory, page table and other levels of page splitting mechanism, but the basic working principle will not change.

The implementation of the paging mechanism requires hardware implementation. the hardware name is MMU (Memory Management Unit), which is specialized in converting virtual addresses to physical addresses, that is, find the physical page from the virtual page.

References:

Deep understanding of Linux kernel

Programmer self-cultivation

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.