Pages and page tables
Video 039 of 140 in Virtualization Foundations, in the unit “Memory: RAM, addresses and virtual memory”. 4:04, with English and Hindi audio.
A program uses memory as one long range of addresses, but the kernel does not track memory one byte at a time. It cuts memory into fixed-size blocks called pages, and it keeps a table that says where each page really sits in RAM. This article explains why pages exist, how a page table finds the real place for a byte, why the map has several levels, and how to check the page size on an Ubuntu server.
What you will learn
- Memory is cut into fixed-size pages, four kibibytes on usual 64-bit x86 Linux.
- A page table maps each page to its real place in RAM.
- Blocks, not single bytes, keep the table small.
- Bigger pages need fewer entries, but each one is handled as a whole.
Why the kernel maps blocks, not single bytes
Suppose the page table had one entry for every byte. A machine with sixteen gibibytes of RAM would need about seventeen billion entries. Even at eight bytes per entry, that table would be well over a hundred gigabytes. So the kernel maps blocks instead, with one entry per block.
What a page is and why it is handled whole
On 64-bit x86 Linux, the usual block, called a page, is four kibibytes. A kibibyte is 1,024 bytes, so a page holds 4,096 bytes. A page is always given whole, so a program that needs only a few bytes still takes a whole page.
How the processor finds one byte
The processor works from a virtual address, which is the address a program uses. It follows these steps:
- Split the address into four index numbers and an offset. The offset gives the position inside a page.
- Walk the tables. The four index numbers walk down four tables, and each entry points to the next table.
- Read the page entry. The last entry holds the real address of the page in RAM.
- Add the offset. The offset is added to that address, which gives the exact byte in RAM.
Why the map has several levels, and what the map costs
A program's address range is huge, but it uses only a few patches of it, such as its code and its data. The kernel builds the lower tables only for the patches in use. Empty parts of the range need no tables at all, so the map stays small, and table memory is spent only where the program really uses space.
The map is not free, though. Many small pages on a big machine need a large map, and that map takes RAM of its own. You can see the map's own cost in the PageTables line of the meminfo file, and watch whether it grows over time.
Note: Pages are a trade-off. Fewer, bigger pages need a smaller map, but a program that needs only a few bytes still takes a whole page.
How to check the page size on Ubuntu
On Ubuntu, the getconf command, asked for PAGESIZE, prints the size of one page in bytes.
getconf PAGESIZEThe meminfo file, in the proc folder, has lines about huge pages. These are bigger pages that the kernel has set aside, if any.
grep -i huge /proc/meminfoTry it yourself
You need an Ubuntu machine. You will see the size of one memory page in bytes, then any huge page lines that the kernel reports.
Print the size of one memory page, in bytes.
getconf PAGESIZEWhat you should see: Prints the page size in bytes.
Show the huge page lines in the meminfo file. These are bigger pages the kernel has set aside, if any.
grep -i huge /proc/meminfoWhat you should see: Shows the huge page lines: bigger pages the kernel has set aside, if any.
Terms
- Page
- A fixed-size block of memory, the unit the kernel maps and hands out.
- Page table
- The table that maps each page of a program's addresses to its real place in RAM.
- Kibibyte (KiB)
- One thousand twenty-four bytes, counted in powers of two.
- Offset
- The position of one byte inside a page.
Check yourself
Why does the kernel map pages instead of single bytes?
One entry per byte would make a huge table. One entry per block of memory keeps the table small.
How many bytes are in one usual page on 64-bit x86 Linux?
Four kibibytes, which is 4,096 bytes.
What does the processor do with the offset in an address?
It adds the offset to the real address of the page, which gives the exact byte in RAM.
Previous: Why every program gets its own address space. Next: The TLB: a cache for address translation.