Huge pages and fragmentation
Compare translation reach with allocation granularity, and distinguish virtual contiguity from the physical layout needed for large mappings.
On this page
The question for today
What you’ll understand
- Calculate page counts and idealized translation reach for two page sizes.
- Distinguish internal waste from physical fragmentation.
- Compare transparent huge pages with explicit HugeTLB allocation before measuring a workload.
A larger page can let one translation describe far more memory, reducing translation overhead for some workloads. That benefit comes with a larger allocation and management granularity. Huge-page engineering is therefore a tradeoff among useful coverage, physical availability, copying and zeroing work, and latency. The right answer depends on access density and measured behavior, not on choosing the largest supported size.
Bigger translations cover more bytes
Assume hardware can represent both 4 KiB and 2 MiB page mappings. One 2 MiB mapping covers the same byte range as 512 base pages. A translation cache with an illustrative fixed number of entries can cover more bytes when its entries refer to larger pages. Actual processors often have different structures and capacities for different page sizes, so multiplying one common entry count is a teaching comparison, not a complete hardware model. Inspect the processor documentation and measure translation events relevant to the deployed configuration.
A large virtual mapping does not automatically become one large hardware page. It may still be backed by many base pages and their leaf entries. Conversely, a physical allocation can be larger than a base page without every supported kernel and architecture representing it through one higher-level hardware entry. Linux supports multiple mechanisms and page sizes. Report the observed mapping and allocation state, not merely an application hint. A successful advice call is not, by itself, evidence that every eligible byte is currently covered by the desired huge mappings.
Internal waste differs from missing contiguous frames
Internal fragmentation is space within an allocated unit that the workload does not use. Touching a tiny fraction of many large regions can allocate far more backing than useful data, depending on the huge-page policy and its later splitting behavior. External physical fragmentation is different: sufficient free base pages may exist in total but not in a contiguous aligned run suitable for a particular large mapping. A contiguous virtual array can still be built from scattered base frames, so virtual continuity does not prove availability of a huge physical extent.
Compaction attempts to move eligible data so that free physical pages can be consolidated. Some pages cannot move under the relevant constraints, and compaction itself consumes time. A policy that permits synchronous work can produce a latency spike when an application asks for memory. Explicit HugeTLB pools have reservation and management rules distinct from transparent huge pages; they should not be described as the same allocator with a different flag. Check supported sizes, pool state, permissions, and deployment limits before relying on explicit allocations to succeed.
Measure translation savings and allocation tails together
A dense, long-lived model buffer may amortize setup work and benefit from reduced translation pressure. A short-lived sparse pool may instead pay zeroing, copying, or waste costs without enough reuse to recover them. Compare the same workload with recorded page-size policies and actual page-size counters. Include peak physical memory, fault behavior, CPU cost, and allocation latency percentiles alongside throughput. A change that improves a steady-state benchmark can still violate a service's startup deadline or make memory packing less efficient across concurrent workers.
Use the probability lab for a narrow allocation-success thought experiment. Set twenty independent requests and success probability 0.8; predict sixteen modeled successful large allocations. Change probability to 0.5 and predict ten. Here probability is a supplied assumption, not a value inferred from total free RAM. Real requests compete for a changing physical layout, so their outcomes are correlated and the binomial model is not a fragmentation simulator. Explain which allocator statistics and controlled failure observations would be needed before assigning an empirical success probability.
Work it through
Translation coverage versus sparse waste
Compare a fully populated aligned 1 GiB region using 4 KiB or 2 MiB mappings. Separately, suppose an allocator backs each of 100 sparse regions with one complete 2 MiB huge page, although only 4 KiB in each is useful.
The dense GiB requires 1,073,741,824 / 4,096 = 262,144 base pages, or 1,073,741,824 / 2,097,152 = 512 huge pages. The mapping-count reduction is 512-fold in this comparison.
The sparse example allocates 100 × 2 MiB = 200 MiB. Its useful payload is 100 × 4 KiB = 400 KiB, or 0.390625 MiB.
Utilization is 400 KiB / 204,800 KiB = 1/512, about 0.1953 percent. That result follows from the stipulated full-huge-page backing; a policy that falls back, splits, or avoids sparse allocation changes the outcome.
Dense coverage and sparse waste can favor different decisions. A lower mapping count is not sufficient evidence of a lower total cost.
The living notebook
Explore the model
Interactive lab · probability
Allocation success is an assumption to test
Set p=0.8 and n=20 for independent hypothetical large-allocation requests; predict sixteen successes. Compare p=0.5. The exact binomial distribution does not know free-list topology, compaction, allocation order, or huge-page policy.
- Mean · np
- 5
- Variance · np(1−p)
- 2.5
- Total probability
- 1
These are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forFree bytes alone do not determine availability of the contiguous aligned extents required by a particular large mapping.
Pause & reason
Check your understanding
Your turn
Try it yourself
For a hypothetical translation cache with 64 entries available for either page size, calculate ideal reach at 4 KiB and 2 MiB. Why should the result not be reported as a real processor specification?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Multiply entry count by page bytes in each separate model.
Compare with the solution
The model gives 64 × 4 KiB = 256 KiB and 64 × 2 MiB = 128 MiB of reach, a 512-fold ratio. Real processors may have different entry counts, associativity, sharing, and lookup paths for each page size. The calculation isolates page-size scaling and must be checked against actual architecture documentation.
Keep the idea
Take it with you
- Larger mappings can reduce translation work for dense reusable memory.
- Internal waste, physical fragmentation, and virtual contiguity are different concerns.
- Observe actual page sizes and allocation latency, not just configuration intent.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.