Linux segment page-memory management

Source: Internet
Author: User

Http://blog.chinaunix.net/uid-24227137-id-3723898.html

A page is a physical unit of information that is paged out in order to implement discrete allocations to reduce the amount of memory that can be made, and to increase the utilization of memory from; or, paging is due to the need for system management, not the needs of the user. Short is the logical unit of information that contains a set of information that is relatively complete in its meaning. The purpose of segmentation is to be able to better meet the needs of users.

How the process uses memory

5 different data segments are involved in any common process,

Code snippet: Storing code

Data segments: Variables and global variables that are statically allocated by the program

BSS: Uninitialized global variable

Heap: Used to store the dynamically allocated memory segments in the process run, and the size is not fixed. When malloc allocates memory, the newly allocated memory is added to the heap (heap expansion), and free frees memory when it is called heap reduction.

Stack: Stores local variables, arguments passed in when a function is called, and parameters returned are stored in the stack.

: Data segments, BSS, heap segments are usually stored continuously, and memory locations are contiguous. Code and stacks are often stored independently. The stack grows downward and the heap grows upward.

Process Memory Space

Using virtual memory management technology in Linux, the user sees and touches the virtual address and cannot see the actual physical memory address. The use of virtual address allows the user to not directly access the physical memory, the process is not interfering with each other, play a role in protection; On the other hand, the user program can use a larger address space than the actual physical memory. The 4G process space is divided into two parts, 1-3g is the user space, 4G is the kernel space, the user usually cannot access the kernel space, and the kernel space can be accessed only when the user process makes system call.

Process Memory Management

The object of process memory management is the memory image on the inbound linear address space, which is actually the virtual memory area used by the process. The virtual space of a process is a 32-bit or 64-bit address space, and the size of the space depends on the architecture. If you want to see the area of memory occupied by a process, you can get it using/proc/maps under Linux.

Note: You will certainly find that the process space contains only three memory areas, there is no heap, BSS mentioned above, in fact, the program memory segment and the process address space in the memory area of the data segment is a fuzzy correspondence, that is, the heap, BSS, data segments (initialized) are in the process space by the data segment memory region represented.

Allocation and recycling of process memory?

Create the Process fork (), the program loads EXECVE (), the mapping file Mmap (), and the Dynamic memory allocation malloc ()/BRK () All need to allocate memory to the process. However, the process applied and obtained is not the actual memory, but the virtual memory, the exact "memory area", the process of the allocation of memory area will eventually be attributed to the Do_mmap () function.

The address that the process can manipulate directly is the virtual address, when the process needs memory, from the memory is only the virtual memory area, rather than the actual physical address, the process does not obtain physical memory, only the use of a new linear address access. Actual physical memory only when the process really accesses the newly acquired virtual address, there will be a "request page mechanism" to produce a fault, the virtual memory mechanism to survive the basic guarantee, it will tell the kernel to actually allocate the physical page for the process, and establish the corresponding page table, The virtual address is actually mapped to the physical memory of the system according to the page table mechanism.

Page replacement Algorithm (LRU): Least Recently used algorithm

In the process of address mapping, if you find that the page you want to access is not in memory, a page fault is generated. When a page break is interrupted, the OS must select one of the pages to move it out of memory, and the rule to choose which page to retire is called the page replacement algorithm. The LRU algorithm selects the longest unused page in a past period of time. The problem of how LRU algorithm is implemented requires actual hardware support, and the implementation of counters and stacks will result in the software overhead of multiple accesses to memory, and the real-time algorithm using LRU approximation is implemented.

Static links and dynamic links

Both static and dynamic links are ways to share code.

With the dynamic link library, the instructions in Lib are all directly included in the resulting EXE file. However, if the use of Dll,dll does not have to be included in the final EXE file, dynamic linking means not linking all program functions to a file when the executable is generated, because there are many functions in the DLL file of the operating system, which is found directly from the operating system when the program is running. The static link library is all the functions used to link to the exe file.

Linux segment page-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.