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.
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!

Zellij: A Modern Terminal Multiplexer for Enhanced Linux Workflows Linux terminal multiplexers are indispensable tools for developers and system administrators, streamlining command-line interactions. Zellij, a relatively new open-source multiplexer

The startup process of Linux includes: 1. Start BIOS/UEFI, 2. Load GRUB, 3. Load kernel and initrd, 4. Execute init process, 5. Start system services, 6. Start login manager; the startup process of Windows includes: 1. Start BIOS/UEFI, 2. Load WindowsBootManager, 3. Load winload.exe, 4. Load tonskrnl.exe and HAL, 5. Start system services, 6. Start login screen; Linux provides more customization options, while Windows pays more attention to user experience and stability.

This guide details how to configure automatic service restarts in Linux using systemd, enhancing system reliability and minimizing downtime. System administrators often rely on this functionality to ensure critical services, such as web servers (Apa

As Linux users, we often rely on commonly used commands ls, grep, awk, sed and find to complete the work. But Linux has a large number of lesser-known commands that can save time, automate tasks and simplify workflows. This article will explore some underrated but powerful Linux commands that deserve more attention. rename – efficient batch rename files The rename command is the savior when you need to rename multiple files at once. Without using loops containing mv, rename allows you to easily apply complex renaming patterns. Change all .txt files to .log. rename 's/\.txt$/\.log/' *

Linux systems provide various system services (such as process management, login, syslog, cron, etc.) and network services (such as remote login, email, printer, web hosting, data storage, file transfer, domain name resolution (using DNS), dynamic IP address allocation (using DHCP), and so on). Technically, a service is a process or group of process (usually known as a daemon) that runs continuously in the background, waiting for incoming requests (especially from the client). Linux supports different ways to manage services (start, stop, restart, enable automatic startup at system startup, etc.), usually through a process or service manager. Almost all modern Linux distributions now use the same

Run Windows Software and Games on Linux with CrossOver 25 Running Windows applications and games on Linux is now easier than ever, thanks to CrossOver 25 from CodeWeavers. This commercial software solution lets Linux users run a wide variety of Wind
![pCloud - The Most Secure Cloud Storage [50% Off Offer]](https://img.php.cn/upload/article/001/242/473/174580357418126.jpg?x-oss-process=image/resize,p_40)
Secure Your Data with pCloud: A Comprehensive Guide to Linux Installation pCloud, a leading secure cloud storage service, provides a robust platform for managing your files and data. This guide details the installation process on Linux systems. About

MangoHud: A powerful tool for real-time monitoring of Linux gaming performance MangoHud is a powerful and lightweight tool designed for gamers, developers, and anyone who wants to monitor system performance in real time. It acts as an overlay for Vulkan and OpenGL applications, displaying important information such as FPS, CPU and GPU usage, temperature, etc. This article will explore the functions, working principles and usage of MangoHud, and provide step-by-step instructions for installing and configuring MangoHud on Linux systems. What is MangoHud? MangoHud is an open source project available on GitHub and aims to provide a simple and customizable way to monitor


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
