Linux Memory Management Beginner

Source: Internet
Author: User

Virtual memory Model

L The Inux kernel itself is not running in virtual space, it uses physical addressing mode.

Physical memory is divided into interfaces, and the size of a memory page is determined by the Page_size macro.

The way virtual address space enables programmers to use huge structures for continuous addresses, regardless of physical memory limitations.

linear address to Physical address

The linear address needs to be converted by a processor or a separate MMU to a physical address, as follows:

The parsing method is:

1. Use the first bit segment in the linear address as the subscript a table item that can be found in the page directory, which points to an intermediate directory.

2. Use the second bit segment in the linear address as the subscript you can find a table entry in the intermediate directory that points to a page table.

3. Use the third bit segment in the linear address as the subscript you can find a table entry in the page table that points to a physical page in physical memory.

4. The fourth bit segment in the linear address is the offset, and the secondary offset is added to the base address of the physical page to obtain the corresponding physical addresses.

Note: The page directory base address is stored in the TLB.

The process of resolving the linear address of kernel space to physical address is relatively simple, because the system itself to maintain a page table is a very disgusting thing, so the address and physical address of the kernel space is a simple direct mapping, 0xc0000000 is the offset of the two.

In other words: for system space, given a virtual address X, its physical address is X minus page_offset; correspondingly, given a physical address X, its virtual address is x+page_offset.

Of course, can not be used to do simple mapping, because if the memory has 4G, then if directly all do simple mapping, then the kernel can only access 0-1g memory, so the kernel is actually the first 896M to do direct mapping, the remaining to do vmalloc application area, the permanent memory map area, fixed mapping area and so on. Such as:

several creation process functions

F ork: the child process simply replicates the parent process's resources, the two process memory spaces are independent, and the runtime is parallel. To reduce the overhead of the runtime, write-time replication is used for resources (that is, fork does not copy resources directly, but only when the write is completed).

V Fork: the shared address space is created after the process parent process blocks until the child process ends.

C Lone: with parameters, you can selectively copy the data to the child process, and the rest is shared by the pointer.

the difference between several functions that request memory

1. Kmalloc and Vmalloc allocate memory for kernel space, and malloc allocates memory for user space.

2. kmalloc Physical Continuous, vmalloc virtual address continuous.

3. Kmalloc can allocate a limited amount of space, Vmalloc and malloc can be allocated larger.

4. Physical continuity is required in the presence of DMA.

5. Kmalloc faster

6. the Kmalloc uses the slab mechanism, and the return address has already corresponded to the actual memory.

Missing pages Interrupt

If the MMU processor does not have access to a page, it will result in a fault. Linux page faults The program must be able to differentiate between programming-induced exceptions caused by references to pages that are not yet assigned a physical page box, and assign a new one if it is legal.

Each process has its own page table.

Page Management Request a page break

to save memory, the operating system only loads the virtual pages that are being used by the executing program. For example, a database program might want to perform a query on a database, not all of the contents of the database are loaded into memory, but only those parts that need to be loaded. This technology, which is only loaded into the interface that is being accessed, is called a request for paging .

When a process tries to access a virtual address that is not in memory, it triggers a Missing pages Interrupt .

If an error address is invalid, such as a random write operation, the operating system terminates the process, and the error of this process does not affect other processes.

If the address in error is valid, the page it accesses is not in memory. The operating system must read this interface from the disk image. The time of the visit is longer, and the process must wait until the page is taken out. The page that is taken is placed in a free physical page box, and the entry for this virtual page box number is added to the page table of the process. The process then starts running from the wrong place.

Exchange

If a process requires a virtual page to be paged into physical memory and the system does not have an idle physical page, the operating system must discard some of the pages in the system to make room for it.

If the interface that is discarded from physical memory comes from an executable or data file on disk and has not been modified, then you do not need to save those interfaces. When the process needs this page again, it can be read directly from the executable file or the data file.

However, if the page has been modified, the system must save it for re-access. This interface is called the Dirty interface. When moved out of memory, they must be saved in a special file called a swap file . Access to swap files is slow relative to CPU and memory speed, so the operating system must measure the dirty pages to swap or leave them in memory to make a choice.

if the algorithm of the discarded page is not good enough, it is possible for the page to be written and read back from disk to make it less efficient. Linux Use the least recently used (LRU) page aging algorithm to fairly choose which pages will be discarded from the system. This algorithm sets an age for each page, which varies depending on the number of pages visited. The more pages are accessed, the younger they are, and the older they are.

LInux High-speed buffering buffer Cache (buffered cache)

The buffer cache contains the data buffers used by the block device. These buffers contain blocks of data that are read from the device or written to the device. If the data can be found in the buffer cache, there is no need for the system to actually read on the physical block device.

paged Cache (page cache)

Page caching is the disk cache used by page I/O operations to access data. As we'll see in the filesystem, read (), write (), and mmap () system call access to regular files is done through the page cache.

The difference between buffer cache and page cache: Buffer cache is the cache for a block device, and page caching is the cache used to do the file system, that is, it directly remembers the files we open (for example, using the two-time Man command, which can obviously feel faster for the second time).

Swap cache (swap cache)

is the swap space in-memory cache. When the file written to the swap space is not modified again, the next time you discard it, you do not need to write the swap space again, this is done through the swap cache.

Attention:

Using the top command will reveal the concept of buffer and cache:

B Uffer: Simply to be written to disk, disk and main memory speed is different, so need buffer to do a middle layer, write the data to write the buffer first, so that the writing process does not need to be caught waiting.

C ache (cache for non-CPU and main memory): simply read from disk and store in cache for later reuse. is often used on disk I/O requests, such as when a file is accessed, it is placed in the cache for future process access.

C both ache and buffer need to be memory-intensive.

Original:

A buffer is something that have yet to being "written" to disk. A cache is something that have been "read" from the disk and stored for later use.



From for notes (Wiz)

Linux Memory Management Beginner

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.