OS & Systems FundamentalsMemory Management & Virtual MemoryMedium⏱️ ~3 min

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.

⚠️ Key Trade-off: Huge pages trade memory efficiency for TLB efficiency. 2 MB pages can waste up to 2 MB per allocation but reduce TLB misses by 500x. Best for large, long lived allocations in memory bound workloads.
💡 Key Takeaways
4 KB pages: minimize fragmentation but only 4 MB TLB coverage with 1024 entries
2 MB pages: 500x TLB coverage improvement, each entry covers 512 standard pages
1 GB pages: cover 1 TB with 1024 entries but require contiguous physical memory
Fragmentation risk: 1 MB allocation with 2 MB pages wastes 50% of the page
Use huge pages for large working sets; standard pages when memory is scarce
📌 Interview Tips
1Calculate TLB coverage comparison: 4 KB times 1024 equals 4 MB vs 2 MB times 1024 equals 2 GB. Show the 500x improvement
2When discussing databases or caches with large working sets, recommend huge pages for TLB efficiency
3Explain internal fragmentation: a process with many 100 KB allocations on 2 MB pages wastes 95% of each page
← Back to Memory Management & Virtual Memory Overview