Home >System Tutorial >LINUX >No need to worry about memory issues anymore—an introduction to Linux system memory management
With the development of computers, the complexity and data volume of software applications are increasing, and the requirements for system memory are also getting higher and higher. As a Linux system administrator, it is essential to understand how to properly manage and allocate memory resources. This article will introduce you to the basic knowledge of Linux system memory management.
Early programs ran directly on the physical address, which means that the space required by the program does not exceed the physical memory of the machine and there will be no problem. However, in actual scenarios, there are multiple tasks and multiple tasks. For processes, it is unreliable that this kind of physical address is reserved for each process.
For example: If there are three programs a, b, c, a needs 10M, b needs 100M, and c needs 20M, the total memory will be 120M. According to the previous allocation method, the first 10M will be given to a, and 10M-110M will be given to b. The system has 10M left, but c needs 20M. Obviously, the remaining memory is not enough for c. what to do?
Why memory management is needed:
1. Efficiency issues
You may think of writing the data of program b to the disk when program c is running, and then writing the data back from the disk when program b is run. Not to mention that it cannot meet the needs of parallel running of programs b and c. Even The time-consuming problems caused by frequent IO operations are also unacceptable.
2. Process address isolation problem
In addition to efficiency issues, the space reserved for the process will crash if it needs to be accessed by other processes. For example, the space accessed by process a is the first 10M, but if there is a piece of code in program a that accesses 10-110M, it may cause program b to crash, so the address spaces of processes need to be isolated from each other.
3.Relocation problem
In real scenarios, it is impossible for a single task to run in allocated memory. When multiple tasks are running in parallel, it is possible to apply for addresses in other processes when dynamically applying to release memory. At this time, you need to relocate to new address.
Memory management is nothing more than finding ways to solve the above three problems. How to improve memory usage efficiency? How to make a process's address space isolated? How to solve the relocation problem when the program is running?
How memory management maps from virtual addresses to physical addresses:
The process of memory management mapping from virtual addresses to physical addresses is also the process of solving the above three problems. Memory management uses segmentation mechanism and paging mechanism to solve the above three problems respectively. The approximate process is as follows:
Segmentation mechanism:
As long as the program is divided into segments and the entire segment is moved to any position, the address within the segment remains unchanged relative to the segment base address. No matter what the segment base address is, as long as the segment offset address is given, the CPU can Access the correct instructions. Therefore, when loading a user program, just copy the contents of the entire segment to a new location, and then change the address in the segment base address register to this address. The program can run accurately because the intra-segment offset address is used in the program. , relative to the new segment base address, the content at the offset address is still the same.
It can be seen that the segmentation mechanism solves the problems of isolation and relocation between processes. This action is done in hardware, but some hardware does not have a segmentation mechanism. As a cross-platform, Linux uses a more versatile paging mechanism to solve the conversion from linear address to virtual address to physical address.
Paging mechanism:
You can refer to "How does the CPU access memory?" 》Understand the concept of one-level page table. In order to be compatible with 32-bit and 64-bit, Linux usually uses four-level page table, page global directory, page superior directory, page intermediate directory, page table:
I will not explain in detail how Linux uses four-level page tables to convert linear addresses (virtual addresses) to physical addresses.
When the process switches, it finds the pgd field in the mm_struct according to the task_struct, obtains the page global directory of the new process, and then fills it into the CR3 register, completing the page switch.
Let’s take a hands-on look at the process of mmu paging and addressing:
Code:
It can be seen that the physical address corresponding to the virtual address ffff99b488d48000 is 80000000c8d48000. This process is also the process of mmu.
Through the introduction of this article, we have learned the basic knowledge of Linux system memory management, including memory partitions, virtual memory, swap space, etc. In actual work, correct memory management can improve the stability and performance of the system and avoid unexpected failures due to memory problems. I hope the introduction in this article can help you better understand the principles and methods of Linux system memory management, allowing you to easily deal with various memory management problems.
The above is the detailed content of No need to worry about memory issues anymore—an introduction to Linux system memory management. For more information, please follow other related articles on the PHP Chinese website!