<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Memory Management]]></title><description><![CDATA[Memory Management]]></description><link>https://memory-management.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 04 Dec 2026 23:07:29 GMT</lastBuildDate><atom:link href="https://memory-management.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[The Hidden Cost of Memory: TLBs, Page Tables, and Huge Pages]]></title><description><![CDATA[Every memory access your program makes passes through a translation layer that converts virtual addresses to physical ones. This translation is so fast and…]]></description><link>https://memory-management.hashnode.dev/the-hidden-cost-of-memory-tlbs-page-tables-and-huge-pages</link><guid isPermaLink="true">https://memory-management.hashnode.dev/the-hidden-cost-of-memory-tlbs-page-tables-and-huge-pages</guid><category><![CDATA[memory-management]]></category><category><![CDATA[HugePages]]></category><category><![CDATA[TLB]]></category><category><![CDATA[os]]></category><dc:creator><![CDATA[shatakshi mishra]]></dc:creator><pubDate>Tue, 31 Mar 2026 06:39:57 GMT</pubDate><content:encoded><![CDATA[<hr />
<h2>Introduction</h2>
<p>Every memory access your program makes passes through a translation layer that converts virtual addresses to physical ones. This translation is so fast and so transparent that most developers never think about it — until it becomes the bottleneck. TLB misses, page table walks, and page size choices can silently shave 5–30% off your application's throughput, and the effect gets worse as working sets grow into the tens or hundreds of gigabytes.</p>
<p>This post dissects the full virtual-to-physical translation path, explains where cycles are lost, and shows how huge pages can recover them — along with the trade-offs you need to understand before flipping the switch in production.</p>
<hr />
<h2>1. The Virtual Memory Contract</h2>
<p>Modern operating systems give every process a flat, isolated virtual address space. The CPU's Memory Management Unit (MMU) translates each virtual address to a physical frame at runtime. The data structure that backs this translation is the <strong>page table</strong>.</p>
<h3>Why paging exists</h3>
<ul>
<li><p><strong>Isolation</strong>: Process A cannot touch Process B's memory.</p>
</li>
<li><p><strong>Overcommit</strong>: The OS can promise more virtual memory than physically available.</p>
</li>
<li><p><strong>Fragmentation avoidance</strong>: Physical frames need not be contiguous.</p>
</li>
<li><p><strong>Demand paging</strong>: Pages are allocated on first access, not at <code>mmap</code> time.</p>
</li>
</ul>
<p>The unit of translation is a <strong>page</strong> — on x86-64, the default size is <strong>4 KiB</strong>.</p>
<hr />
<h2>2. The x86-64 Page Table: A Four-Level Radix Tree</h2>
<p>A 64-bit virtual address is 48 bits wide on current x86-64 hardware (with 5-level paging extending this to 57 bits). The MMU splits the address into indices that walk a four-level radix tree:</p>
<pre><code class="language-plaintext"> 63       48 47    39 38    30 29    21 20    12 11       0
┌──────────┬────────┬────────┬────────┬────────┬──────────┐
│  Sign Ext│ PML4   │  PDPT  │   PD   │   PT   │  Offset  │
│ (16 bits)│(9 bits)│(9 bits)│(9 bits)│(9 bits)│ (12 bits)│
└──────────┴────────┴────────┴────────┴────────┴──────────┘
</code></pre>
<p>Each level is a 512-entry table (2^9) of 8-byte entries. A full walk looks like this:</p>
<ol>
<li><p><strong>CR3</strong> register points to the physical address of the PML4 table.</p>
</li>
<li><p>PML4 entry → Page Directory Pointer Table (PDPT).</p>
</li>
<li><p>PDPT entry → Page Directory (PD).</p>
</li>
<li><p>PD entry → Page Table (PT).</p>
</li>
<li><p>PT entry → Physical frame. Concatenate with the 12-bit offset.</p>
</li>
</ol>
<p><strong>That's four memory accesses just to resolve one virtual address.</strong> At ~100 cycles per DRAM access, an uncached page walk costs roughly <strong>400 cycles</strong> — compared to ~4 cycles for an L1 cache hit. This is where the TLB saves you.</p>
<hr />
<h2>3. The TLB: Your Most Important Cache</h2>
<p>The <strong>Translation Lookaside Buffer</strong> is a small, fully-associative (or set-associative) cache inside the CPU that stores recent virtual-to-physical translations. On a TLB hit, the translation completes in <strong>~1 cycle</strong>, entirely bypassing the page table.</p>
<h3>TLB geometry on modern x86 CPUs</h3>
<table>
<thead>
<tr>
<th>Level</th>
<th>Entries (4 KiB)</th>
<th>Entries (2 MiB)</th>
<th>Entries (1 GiB)</th>
<th>Associativity</th>
</tr>
</thead>
<tbody><tr>
<td>L1 dTLB</td>
<td>64</td>
<td>32</td>
<td>—</td>
<td>4-way</td>
</tr>
<tr>
<td>L1 iTLB</td>
<td>128</td>
<td>8</td>
<td>—</td>
<td>8-way</td>
</tr>
<tr>
<td>L2 sTLB</td>
<td>1536–2048</td>
<td>1536–2048</td>
<td>—</td>
<td>6–12 way</td>
</tr>
</tbody></table>
<p><em>(Values approximate; varies by microarchitecture. Shown for Intel Alder Lake / Sapphire Rapids era.)</em></p>
<h3>Key insight: coverage, not capacity</h3>
<p>With 4 KiB pages:</p>
<ul>
<li><p><strong>L1 dTLB</strong>: 64 entries × 4 KiB = <strong>256 KiB</strong> of coverage</p>
</li>
<li><p><strong>L2 sTLB</strong>: 2048 entries × 4 KiB = <strong>8 MiB</strong> of coverage</p>
</li>
</ul>
<p>With 2 MiB huge pages:</p>
<ul>
<li><p><strong>L1 dTLB</strong>: 32 entries × 2 MiB = <strong>64 MiB</strong> of coverage</p>
</li>
<li><p><strong>L2 sTLB</strong>: 2048 entries × 2 MiB = <strong>4 GiB</strong> of coverage</p>
</li>
</ul>
<p>If your working set is 1 GiB — common for databases, JVMs, and ML inference — the 4 KiB TLB can cover less than 1% of it. Every access outside that window triggers a page table walk.</p>
<hr />
<h2>4. Anatomy of a TLB Miss</h2>
<p>When a translation is not in the TLB, the CPU initiates a <strong>hardware page walk</strong>. On x86, this is done by a dedicated Page Miss Handler (PMH) unit — not by the OS. The walk proceeds through all four page table levels, potentially hitting the CPU caches along the way.</p>
<h3>Page walk latency depends on where the page table entries land</h3>
<table>
<thead>
<tr>
<th>PTE location</th>
<th>Approximate walk latency</th>
</tr>
</thead>
<tbody><tr>
<td>L1 cache</td>
<td>~10 cycles</td>
</tr>
<tr>
<td>L2 cache</td>
<td>~25 cycles</td>
</tr>
<tr>
<td>L3 cache</td>
<td>~50 cycles</td>
</tr>
<tr>
<td>DRAM</td>
<td>~200–400 cycles</td>
</tr>
</tbody></table>
<p>On a NUMA system, accessing page table entries in remote DRAM can push this to <strong>600+ cycles</strong>.</p>
<h3>The hidden multiplier: page walks compete for cache</h3>
<p>Page table entries are cached in the regular L1/L2/L3 data caches. Under heavy TLB miss rates, the page walker pollutes the data cache with page table entries, evicting actual application data. This creates a vicious feedback loop:</p>
<ol>
<li><p>Large working set → TLB misses.</p>
</li>
<li><p>TLB misses → page walks that load PTEs into cache.</p>
</li>
<li><p>PTE loads evict application data from cache.</p>
</li>
<li><p>Application data cache misses increase.</p>
</li>
<li><p>Go to step 1.</p>
</li>
</ol>
<p>This effect can account for <strong>10–20% of L3 cache occupancy</strong> in workloads with large, randomly-accessed datasets.</p>
<hr />
<h2>5. Huge Pages: The Primary Mitigation</h2>
<p>The most effective way to reduce TLB pressure is to increase the page size.</p>
<h3>x86-64 supported page sizes</h3>
<table>
<thead>
<tr>
<th>Page size</th>
<th>Page table levels walked</th>
<th>TLB coverage (L1 dTLB)</th>
</tr>
</thead>
<tbody><tr>
<td>4 KiB</td>
<td>4</td>
<td>256 KiB</td>
</tr>
<tr>
<td>2 MiB</td>
<td>3 (PD entry has PS bit set)</td>
<td>64 MiB</td>
</tr>
<tr>
<td>1 GiB</td>
<td>2 (PDPT entry has PS bit set)</td>
<td>N/A (limited entries)</td>
</tr>
</tbody></table>
<p>With 2 MiB pages, the walk stops one level earlier — the Page Directory entry directly maps the frame, skipping the final Page Table level. This has two benefits:</p>
<ol>
<li><p><strong>512× more coverage per TLB entry</strong> → fewer misses.</p>
</li>
<li><p><strong>One fewer memory access per walk</strong> → cheaper misses.</p>
</li>
</ol>
<hr />
<h2>6. The Dark Side: When Huge Pages Hurt</h2>
<p>Huge pages are not a universal win. Understanding the failure modes is critical.</p>
<h3>Memory waste from internal fragmentation</h3>
<p>A 4 KiB allocation backed by a 2 MiB huge page wastes 99.8% of the page. The OS cannot reclaim the unused portion. If your application makes many small, sparse allocations (e.g., a process that <code>mmap</code>s thousands of small files), huge pages can dramatically increase RSS.</p>
<h3>THP allocation stalls</h3>
<p>When <code>khugepaged</code> tries to promote a range of 512 contiguous 4 KiB pages into a single 2 MiB huge page, it must:</p>
<ol>
<li><p>Find a contiguous 2 MiB physical region (or compact memory to create one).</p>
</li>
<li><p>Copy 512 pages into the new region.</p>
</li>
<li><p>Update page tables and flush TLBs.</p>
</li>
</ol>
<p>Under memory pressure, step 1 can stall the application for <strong>milliseconds</strong>. This is the primary reason many latency-sensitive systems (Redis, some trading platforms) set:</p>
<pre><code class="language-bash">echo never &gt; /sys/kernel/mm/transparent_hugepage/enabled
echo never &gt; /sys/kernel/mm/transparent_hugepage/defrag
</code></pre>
<h3>TLB shootdowns at scale</h3>
<p>When a huge page is unmapped or its permissions change, the kernel must invalidate the corresponding TLB entry on <strong>every CPU core</strong> that might have cached it. This is done via Inter-Processor Interrupts (IPIs) — called a <strong>TLB shootdown</strong>.</p>
<p>With 4 KiB pages, an <code>munmap</code> of 2 MiB sends one IPI for each of the 512 pages (though the kernel batches these). With a 2 MiB huge page, it's a single IPI, but that single entry covers a much larger range, so stale-TLB bugs become more dangerous.</p>
<p>On systems with many cores (64+), TLB shootdown storms from frequent mmap/munmap cycles can serialize the entire system. This is a known scalability bottleneck in the Linux kernel.</p>
<h3>NUMA interactions</h3>
<p>On multi-socket systems, huge pages can create NUMA imbalances. A 2 MiB page is allocated on a single NUMA node. If threads on the remote node access it, every access pays the cross-interconnect latency (~100 ns penalty). With 4 KiB pages, first-touch NUMA policy distributes pages more granularly across nodes.</p>
<p>Mitigation: use <code>numactl --interleave=all</code> or <code>mbind()</code> with <code>MPOL_INTERLEAVE</code> for huge page regions that will be accessed by threads across multiple nodes.</p>
<hr />
<h2>7. Five-Level Page Tables and the Future</h2>
<p>Intel's Ice Lake server and later support <strong>5-level paging</strong> (LA57), extending the virtual address space from 48 bits (256 TiB) to 57 bits (128 PiB). This adds a fifth level (PML5) to the page walk.</p>
<pre><code class="language-plaintext"> 63       57 56    48 47    39 38    30 29    21 20    12 11       0
┌──────────┬────────┬────────┬────────┬────────┬────────┬──────────┐
│  Sign Ext│ PML5   │ PML4   │  PDPT  │   PD   │   PT   │  Offset  │
│ (7 bits) │(9 bits)│(9 bits)│(9 bits)│(9 bits)│(9 bits)│ (12 bits)│
└──────────┴────────┴────────┴────────┴────────┴────────┴──────────┘
</code></pre>
<p>An uncached walk now costs <strong>5 DRAM accesses</strong> (~500+ cycles). This makes TLBs and huge pages even more important. The additional level also enables <strong>512 GiB pages</strong> via PML5 entries with the PS bit set — though no OS currently supports them and the fragmentation implications are extreme.</p>
<h3>Other architectures</h3>
<ul>
<li><p><strong>ARM (AArch64)</strong>: Supports 4 KiB, 16 KiB, and 64 KiB base page sizes, plus 2 MiB and 1 GiB huge pages (via block descriptors). ARM's 16 KiB page option is an interesting middle ground — used by Apple's M-series chips.</p>
</li>
<li><p><strong>RISC-V (Sv48)</strong>: Four-level page table with 4 KiB base pages and 2 MiB / 1 GiB superpages.</p>
</li>
</ul>
<hr />
<h2>Conclusion</h2>
<p>The virtual memory translation path is one of those "invisible" layers that becomes visible only at scale. For any application with a working set measured in gigabytes, TLB misses are likely in your top-5 performance bottlenecks — you just haven't measured them yet.</p>
<p>The fix is conceptually simple: use bigger pages to cover more memory per TLB entry. But the implementation details matter enormously. Explicit huge pages give you control and predictability. Transparent huge pages give you convenience at the cost of occasional latency surprises. And neither helps if you haven't first confirmed, via hardware performance counters, that TLB pressure is actually your problem.</p>
<p>Measure first. Then choose your page size. Then measure again.</p>
<hr />
]]></content:encoded></item></channel></rss>