Home >System Tutorial >LINUX >Why the default page size of Linux system is 4KB
As we all know, Linux manages memory in units of pages. Whether it is loading data from disk into memory or writing data from memory back to disk, the operating system will operate in units of pages, which also means that if we only write one byte of data to disk , the operating system also needs to flush all the data in the entire page to the disk.
It is worth noting that in Linux, we can either use normal-sized memory pages for operations or use huge memory pages (Huge Page), although the default size of memory pages on most processors is 4KB. But some processors will use 8KB, 16KB or 64KB as the default page size. In addition to normal memory page sizes, different processors support different huge page sizes. For example, on an x86 processor we can use memory pages of 2MB size.
4KB memory pages have become a historical relic. This page size was widely adopted starting in the 1980s and still remains today. Although today's hardware is richer than in the past, we are still stuck with the 4KB memory page size passed down from the past. Normally, we can clearly see the specifications of the memory module when installing the memory, as shown in the figure below:
Figure 1 – Random Access Memory
Today, 4KB memory page size may not be the best choice. 8KB or 16KB may be a better choice, but this is a trade-off made in the past under specific scenarios. In this article, we should not be too obsessed with the number 4KB. We should pay more attention to the several factors that determine this result, so that when we encounter similar scenarios, we can consider the best choice from these aspects. In this article The article will introduce the following two factors that affect the memory page size, they are:
Too small page size will bring larger page table entries to increase the TLB (Translation lookaside buffer) search speed and additional overhead during addressing;
Excessive page size will waste memory space, cause memory fragmentation, and reduce memory utilization;
In the last century, the above two factors were fully considered when designing the memory page size, and finally 4KB memory pages were selected as the most common page size of the operating system. Next, we will introduce in detail the impact of the above on the performance of the operating system. Influence.
We have introduced virtual memory in Linux in the article Why Linux Needs Virtual Memory. What each process can see is an independent virtual memory space. Virtual memory space is only a logical concept. The process still needs to access virtual memory. Memory corresponds to physical memory, and the conversion from virtual memory to physical memory requires the use of page tables held by each process.
In order to store the mapped data of 128 TiB virtual memory in the 64-bit operating system, Linux introduced a four-layer page table to assist virtual address translation in 2.6.10, and a five-layer page table structure was introduced in 4.11. In the future, more layers of page table structures may be introduced to support 64-bit virtual addresses.
Figure 2 – Four-layer page table structure
In the four-layer page table structure shown in the figure above, the operating system will use the lowest 12 bits as the page offset, and the remaining 36 bits will be divided into four groups to represent the current level in the previous layer. Index, all virtual addresses can use the above-mentioned multi-layer page table to find the corresponding physical address.
Because the size of the virtual address space of the operating system is certain, the entire virtual address space is evenly divided into N memory pages of the same size, so the size of the memory page will ultimately determine the level of page table entries in each process. Structure and specific number, the smaller the virtual page size, the more page table entries and virtual pages there are in a single process.
PagesCount=VirtualMemory ÷ PageSize
Because the current virtual page size is 4096 bytes, the 12 bits at the end of the virtual address can represent the address in the virtual page. If the size of the virtual page is reduced to 512 bytes, then the original four-level page table structure or five-level page table structure The layered page table structure will become five or six layers, which will not only increase the additional overhead of memory access, but also increase the memory size occupied by the page table entries in each process.
Because the memory mapping device works at the memory page level, the operating system believes that the smallest unit of memory allocation is the virtual page. Even if the user program only applies for 1 byte of memory, the operating system will apply for a virtual page for it, as shown in the figure below. If the size of the memory page is 24KB, then applying for 1 byte of memory will waste ~99.9939% of the space. .
Figure 3 – Fragmentation of large memory
As the memory page size increases, memory fragmentation will become more and more serious. Small memory pages will reduce memory fragmentation in the memory space and improve memory utilization. In the last century, memory resources were not as abundant as they are today. In most cases, memory is not a resource that limits the running of programs. Most online services require more CPUs, not more memory. However, memory was actually a scarce resource in the last century, so improving the utilization of scarce resources is something we have to consider:
Figure 4 – Price of memory
Memory sticks in the 1980s and 1990s were only 512KB or 2MB, and the price was ridiculously expensive. However, memory of several GB is very common today, so although memory utilization is still very important, the price of memory Today, with significant reductions, fragmented memory is no longer a key problem that needs to be solved.
In addition to memory utilization, larger memory pages will also increase the additional overhead of memory copying. Because of the copy-on-write mechanism on Linux, when multiple processes share the same memory, when one of the processes Modifying the shared virtual memory will trigger the copy of the memory page. At this time, the smaller the memory page of the operating system, the smaller the additional overhead caused by copy-on-write.
Summarize
As we mentioned above, 4KB memory page is the default setting decided in the last century. From today's perspective, this is probably the wrong choice. Arm64, ia64 and other architectures can already support it. Memory pages of 8KB, 16KB and other sizes. As the price of memory becomes lower and lower and the system memory becomes larger and larger, larger memory may be a better choice for the operating system. Let’s review the two Factors that determine memory page size:
Finally, let’s look at some relatively open related questions. Interested readers can think carefully about the following questions:
What are the differences and connections between sectors, blocks and pages in Linux?
How is the block size determined in Linux? What are the common sizes?
The above is the detailed content of Why the default page size of Linux system is 4KB. For more information, please follow other related articles on the PHP Chinese website!