Glibc memory management mode, glibc memory management
The memory space exposed by programmers is different from the physical memory space exposed by the system. Generally, a process faces a linear virtual memory space: the address ranges from 0 to the maximum. Each process has the same virtual memory space and has all the memory addresses. The virtual memory space is linear, but it does not mean continuous. The virtual space of some address segments can be missing (not all addresses can be used to store data ).
The virtual memory can be managed by page. The size of each page is generally 4 kb. There is a physical memory (either primary or secondary) corresponding to each page. In physical memory, we are not called pages, but frames. The advantage of paging is that the secondary storage can be used when the primary storage is insufficient. We can save unused primary storage pages to the secondary storage, so that this primary storage page can be overwritten by us and can be restored from the secondary storage when necessary. When installing linux, if the memory is too small, we often increase the virtual memory space by activating the swap partition. Based on the local principle, the speed is not significantly different.
Not every Virtual Memory Page is backed by an actual memory frame. If there is no corresponding actual memory, the page is missing. There is a non-column: When the virtual page data is all 0, no memory frame is required. You only need a tag.
Memory pages and memory frames are not necessarily in a one-to-one relationship. As mentioned earlier, some memory pages may not have memory frames, some memory frames may be used by Multiple Memory pages (Multiple Memory pages generally belong to multiple processes, and the above mentioned process faces the same space ). In other words, a single memory frame can be shared by multiple memory pages and further shared by multiple processes. A typical example is the library function printf. Each process shares the library function printf (dynamic link library). Therefore, printf actually only has one instance, and the printf function of each process is in the same memory frame.
Although the memory page can be backed by a memory frame, the Memory Page must correspond to a memory frame to effectively access the memory page. The actual requirement is that the memory page is larger than the actual size of the memory frame. The actual reason for the huge demand-every process wants to exclusively occupy the entire physical memory. How can we solve this problem? Think of elevators in real life below. An elevator can accommodate a maximum of 10 people, while a building has 200 people. So do we need 20 elevators? Otherwise, maybe two or three elevators will work. The reason is: people will not always stay in the elevator. Let's take a look at our memory. The Memory Page is quite human, and the memory frame is equivalent to the elevator. If the frame is not enough, the page will be displayed. Memory pages can be stored in secondary storage, such as disks. If we suddenly need to store the memory pages on the secondary storage, then the memory pages on the secondary storage correspond to the actual memory frame. This line can be called a form feed.
The missing page is mentioned above. More strictly speaking, page missing is an error that occurs when a process attempts to access a memory page without a memory frame. This is when the kernel will suspend the process, perform some scheduling to connect the memory page with the memory frame, and then resume the process. This is called feed/faulting in. It is worth noting that this situation is not a good thing and will slow down the running of the program. The page is an IO operation.
Memory application, many people may think this operation is required only when the memory is insufficient. However, if the memory is insufficient, where can you obtain it? In fact, the memory application will track the data address of a process to ensure that the memory of the same address will not store two completely different data.
There are two ways for a process to apply for memory: exec, programming, and [fork].
Exec can create a virtual address space for a process, load the most basic program, and then execute the program. Once the program starts to run, the program uses programming to obtain additional memory. There are two methods to apply for memory in the gnu c Library: automatic application and dynamic application.
Memory ing I/O is a dynamic virtual memory application method. Maps the content of the virtual memory to a regular file on the IO Device. Any modifications to the virtual memory can be synchronized to the regular files on IO. Only when we access the virtual memory of this address segment, do I/O read/write and access the actual memory address. Therefore, this is a very efficient read/write method.
A process can request or release the memory by programming, but you cannot release the memory requested by exec.
The virtual memory is managed by PAGE, while the virtual address of the process is managed by segment. It is collectively referred to as page-breaking management. A segment is a continuous virtual address. Generally, there are three important sections:
1. text segment: contains the program commands, literal values, and static constants. Apply by exec and keep the same size throughout the lifecycle.
2. data segment: The data segment serves as the workspace of the program. You can apply to load exec in advance, or expand or reduce the size of the space by a process programmatically. However, no matter how you expand or contract out, this section has a fixed minimum space.
3. stack segment: the stack segment contains the stack space of a program. The program stack expands, but it does not.