Page recycling and reverse ing in Linux (1)

Source: Internet
Author: User

The operating system manages the physical pages in the memory and is also responsible for memory allocation. Applications can apply for physical pages from the operating system through the memory allocation function. After these physical pages are used, applications can release these physical pages through the corresponding memory release function. However, for some physical pages in the memory, page users do not actively release them. If these physical pages have been occupied and cannot be released, therefore, no matter how much physical memory is available on the computer, the physical memory will be used up sooner or later. Therefore, for physical pages that cannot be automatically released, the operating system needs to provide corresponding functions to release them. in Linux, the page recycling algorithm is provided to recycle pages.

Generally, physical pages used for page cache cannot be released by users of pages because they do not know when these pages should be released. In Linux, the biggest benefit of page cache is that it allows programs to quickly obtain data from the cache, thus improving system performance. When the system load is not heavy, the Linux operating system will allocate a large number of physical pages for page caching to improve program running efficiency. However, when the system load is heavy, the Linux operating system may recycle pages for caching and reduce the allocation of pages for caching to meet the memory allocation requests with higher priority in the system. For a user process, the Linux operating system can allocate physical memory to it as needed, but when the user process no longer needs these physical pages, if a user process does not actively release the occupied pages, the Linux operating system does not force the user process to release these physical pages. Based on the above situation, when there are fewer and fewer physical pages available in the memory, and the memory usage is stretched, in order to ensure the smooth operation of the system, the Linux operating system will collect the physical pages that have been occupied for a long time and are not effectively used according to certain algorithms.

The physical pages used by the operating system kernel itself are not included in the Linux operating system's page recycling considerations. This is because, compared with user processes, the kernel does not need to occupy a lot of memory, recycling the physical pages occupied by the kernel significantly increases the complexity of the kernel code and has low potential benefits.

How to recycle pages

Which pages can be recycled

Not all physical pages in the memory can be recycled. In general, the following physical pages can be recycled by the Linux operating system:

● Pages used to buffer data during file read/write operations

● Pages used for File Memory ing in the user address space

● Anonymous page: the stack in process user mode or the memory zone mapped anonymously using mmap

● It is specially used for slab distributor caching, for example, for caching the dentry cache of the file directory structure and for caching the inode cache of the index node.

Before the page is recycled by the operating system, all associated process page items must be disconnected from the page. For an anonymous page, before the page is recycled, the content on the anonymous page must be first exchanged to the swap area. If the page to be recycled is a "dirty" page, before the page is recycled, you need to write back the data on the page.

In addition, other pages cannot be recycled or recycled at all. For example, the page occupied by the kernel will not be recycled; the page mapped to the kernel space will not be recycled; the page dynamically generated by the kernel during execution needs to be permanently stored in memory; locked pages cannot be recycled. Physical pages that are not occupied do not need to be recycled at all.

Page recycling time

The Linux operating system uses the following two mechanisms to check the system memory usage, so as to determine whether the available memory is too small and thus page recycling is required.

○ Periodic check: this is done by the daemon kswapd running in the background. This process regularly checks the memory usage of the current system. When it finds that the number of idle physical pages in the system is less than the specified threshold, this process will initiate page recycling.

○ Trigger of a "memory shortage" Event: in some cases, for example, the operating system suddenly needs to allocate a large block of memory to user processes through the partner system, or you need to create a large buffer, and the memory in the system could not provide enough physical memory to meet this memory request. At this time, the operating system must recycle the page as soon as possible, in order to release some memory space to meet the above memory requests. This page recycling method is also called "direct page recycling ".

If the operating system still cannot recycle enough pages to meet the above memory requirements, the operating system has only the last option, OOM (out of memory) killer is used to select the most suitable process from the system to kill it and release all the pages occupied by the process.

The memory recovery mechanism described above mainly depends on three fields: pages_min, pages_low, and pages_high. Each memory zone defines these three fields in its region descriptor. The meanings of these three fields are shown in table 1.

Table 1. Field meaning

Name Field description
Pages_min The number of reserved pages in the region. If the number of idle physical pages is lower than pages_min, the system pressure will be relatively high. In this case, the memory area urgently needs idle physical pages, the demand for page recycling is urgent.
Pages_low Controls the minimum page recycling threshold. If the number of idle physical pages is lower than pages_low, the operating system kernel starts page recycling.
Pages_high Control the maximum threshold value for page recycling. If the number of idle physical pages is greater than pages_high, the status of the memory area is ideal.


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.