[Operating system] page size

"Page size is a parameter that the operating system can choose. For example, even if the hardware design only supports 512-byte pages, the operating system can easily be allocated by always assigning page pairs 0 and 1, 2 and 3, 4 and 5 Two consecutive 512-byte page frames, and use it as a 1KB page.

To determine the optimal page size requires a trade-off between several conflicting factors. From the results, there is no global optimal. First, there are two factors that can be used as a reason to choose a small page. Randomly selecting a text segment, data segment, or stack segment is likely not to fill exactly an integer number of pages. On average, half of the last page is empty. The extra space is wasted. This waste is called internal fragmentation. When there are n segments in memory and the page size is p bytes, np / 2 bytes will be wasted by internal fragmentation. From this perspective, it is better to use small pages.

Choosing a small page also has an obvious advantage. If you consider a program, it is divided into 8 stages to execute sequentially, and each stage requires 4KB of memory. If the page size is 32KB, then the process must always allocate 32KB of memory. If the page size is 16KB, it only needs 16KB. If the page size is 4KB or less, it only needs 4KB of memory at any time. In general, large pages keep more useless programs in memory than small pages.

On the other hand, a small page means that the program needs more pages, which in turn means a larger page table. A 32KB program only needs 4 8KB pages, but it needs 64 512 byte pages. The transfer between memory and disk is generally one page at a time. Most of the time in the transfer is spent on seek and rotation delays, so the time it takes to transfer a small page is basically the same as the transfer of a large page. . It may take 64 × 10ms to load 64 pages of 512 bytes, while it may only take 4 × 12ms to load 4 pages of 8KB.

On some machines, every time the CPU switches from one process to another, the page table of the new process must be loaded into the hardware register. In this way, the smaller the page, the longer it will take to load the page register, and the space occupied by the page table will increase as the page decreases.

The last point can be analyzed mathematically, assuming that the average size of the process is s bytes, the page size is p bytes, and each page table entry requires e bytes. Then the number of pages required by each process is about s / p, occupying se / p bytes of page table space. The internal waste of memory on the last page is p / 2. Therefore, the total cost caused by the loss of page tables and internal fragmentation is the sum of the following two:

Overhead = se / p + p / 2

When the page is relatively small, the first item (page table size) is large. The second item (internal fragmentation) is larger when the page is larger. The optimal value must be obtained when the page size is at a value in the middle. By derivating p once and making the right side equal to zero, we get the equation:

-se/p2 +1/2=0

From this equation, a formula for optimal page size can be derived (only considering fragment waste and memory required by the page table), the result is:

Insert picture description here

For s = 1MB and each page table entry e = 8 bytes, the optimal page size is 4KB. The page size used by commercial computers is generally between 512 bytes and 64KB. The typical value used to be 1KB, and the more common page size is 4 KB or 8KB. As the memory grows larger, pages also tend to be larger (but not linear). Expanding the RAM by a factor of 4 rarely doubles the page size.

Published 468 original articles · won 14 · 110,000 views

Guess you like

Origin blog.csdn.net/LU_ZHAO/article/details/105624177