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.
Page table entry
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.
Fragmentation
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:
- Too small a page size will lead to larger page table entries, which will increase the TLB (Translation lookaside buffer) search speed and additional overhead during addressing, but will also reduce memory fragmentation in the program and improve memory utilization;
- Excessive page size will waste memory space, cause memory fragmentation, and reduce memory utilization, but it can reduce page table entries in the process and TLB addressing time;
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!

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

在linux中,rpc是远程过程调用的意思,是Reomote Procedure Call的缩写,特指一种隐藏了过程调用时实际通信细节的IPC方法;linux中通过RPC可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。

在linux中,交叉编译是指在一个平台上生成另一个平台上的可执行代码,即编译源代码的平台和执行源代码编译后程序的平台是两个不同的平台。使用交叉编译的原因:1、目标系统没有能力在其上进行本地编译;2、有能力进行源代码编译的平台与目标平台不同。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
