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

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:

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.

Print the size of one memory page, in bytes
getconf PAGESIZE

The meminfo file, in the proc folder, has lines about huge pages. These are bigger pages that the kernel has set aside, if any.

Show the huge page lines in the meminfo file
grep -i huge /proc/meminfo

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

  1. Print the size of one memory page, in bytes.

    getconf PAGESIZE

    What you should see: Prints the page size in bytes.

  2. Show the huge page lines in the meminfo file. These are bigger pages the kernel has set aside, if any.

    grep -i huge /proc/meminfo

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