Page Size Trade-offs: 4 KB vs 2 MB vs 1 GB
Standard 4 KB Pages
The default page size on most systems is 4 KB. This granularity minimizes internal fragmentation: a 5 KB allocation wastes 3 KB (the unused portion of the second page). Smaller pages mean finer grained memory management and less wasted space for small allocations.
The downside is TLB pressure. With 4 KB pages and 1024 TLB entries, only 4 MB of translations fit in the TLB. A database with a 100 GB working set will have constant TLB misses. Each miss triggers a page table walk costing 200 nanoseconds, making memory access 50x slower.
Huge Pages: 2 MB and 1 GB
Huge pages reduce TLB pressure dramatically. A single 2 MB page replaces 512 standard pages. The same 1024 TLB entries now cover 2 GB instead of 4 MB. For a 100 GB working set, TLB miss rate drops by 500x. Memory bound workloads can see 10 to 30 percent throughput improvements.
1 GB pages go further: 1024 entries cover 1 TB. But allocation becomes harder. The system needs 1 GB of contiguous physical memory. After running for days with fragmentation, finding contiguous 1 GB blocks becomes impossible without compaction that stalls the system.
Internal Fragmentation Cost
Huge pages waste space when allocations do not fill them. A 1 MB allocation with 2 MB pages wastes 1 MB. A 500 KB allocation wastes 1.5 MB. For workloads with many small allocations, memory usage can double or triple.
The trade-off is clear: use huge pages when working set is large and allocations are big. Use standard pages when memory is scarce or allocations are small. Some systems use a mix: huge pages for heap, standard pages for stack and small mappings.