This chapter describes what page sizes, the pros and cons if small vs large page sizes and how some systems can use multiple page sizes at the same time.

14.5. Page Sizes#

Most modern processors that support a paged virtual memory model also support multiple page sizes. There are several benefits and downsides of any page size.

A smaller page size reduces the internal fragmentation, on the average 1/2 of the last page of a memory region will be unused or wasted. Half of a 4KB page is only 2KB but half of a 1GB page is 512MB! Smaller page sizes also make for a more compact virtual address space since pages must be aligned on their size boundaries. When the virtual address space is more compact both text and data regions can be smaller and closer together allowing for more efficient relative addressing instructions to be used. This typically results in fewer memory references because the distance between instructions and data is smaller and fit within an instruction. This is especially true for smaller programs.

A larger page size allows for fewer page structures for a given amount of memory that the memory management system has to manage. This means that fewer page structures will be on various linked lists, etc. which typically results in needing to hold spin locks for shorter periods of time when walking these linked lists. A larger page size also results in significantly more text and data being mapped in translation caches. A cache that has 128 entries can only map 512KB with a 4KB page size where it can map 128GB with a 1GB page size. This alone will practically eliminate translation cache misses once a process is running.

Selecting a page size is a trade-off and there is really no one-size fits all optimally. The Intel x86_64 processor supports 3 page sizes; 4KB, 2MB and 1GB. Most operating systems that will run on this architecture can be built and booted to run any of these page sizes. In addition, the x86_64 can use all 3 pages sizes at the same time within a single address space. Regions of memory that are fairly small(measured in KB) like a stack or heap might use a 4KB page size. Regions of memory that are larger(measured in MB) like a large program text or shared library text might use a 2MB page size. Finally regions of memory that are huge like a data base cache or huge shared mapped files might use a 1GB page size.

../_images/pagesize.png

Fig. 14.21 X86_64 page sizes#

14.5.1. Small page sizes#

14.5.1.1. Pros:#

  1. Less internal fragmentation

  2. Better fit for various data structures, code sections

  3. Less unused program memory

14.5.1.2. Cons:#

  1. Programs need many pages, larger page tables

  2. More TLB misses

  3. Higher memory management overhead

14.5.2. Large page sizes#

14.5.2.1. Pros:#

  1. Smaller page tables

  2. fewer TLB misses

  3. Lower memory management overhead

14.5.2.2. Cons:#

  1. More internal fragmentation

  2. More unused program memory

  3. Worse fit for various data structures, code sections