PagedAttention & KV-Cache Virtual Memory Simulator
Explore how operating system virtual memory paging transforms high-throughput LLM inference. Simulate dynamic physical block allocation, watch logical-to-physical block table translations ($L_i \to P_k$), observe Prefix Caching with Copy-on-Write (CoW), and inspect real-time mathematical comparisons between naive contiguous over-allocation and zero-waste PagedAttention.
Higher batch throughput in identical VRAM
Saved by deduplicating system prompts
Over-reserved for ungenerated tokens
Bounded strictly by tail block slack (< 3.5%)
Contiguous Static Pre-Allocation
Allocators must reserve contiguous virtual memory for the request's maximum possible sequence length (max_tokens) upfront. Ungenerated slots remain locked and unusable by concurrent requests.
PagedAttention On-Demand Allocation
KV-cache is partitioned into fixed-size physical blocks (16 tokens). Blocks are allocated on-demand as tokens decode. Non-contiguous blocks are stitched via a logical-to-physical block table, eliminating all external fragmentation.
Memory per token is calculated as 2 × n_layers (32) × n_heads (32) × head_dim (128) × 2 bytes = 128 KB/token. In contiguous allocation, reserving 100 max tokens allocates 12.8 MB upfront. With PagedAttention (block size 16), only 2048 KB is allocated at a time. Internal fragmentation is bounded by (B - (tokens % B)) < 16 tokens per sequence, yielding a sustained 2.2× concurrency boost.
128 KB/tok•Active: 4 reqsGPU VRAM Physical Memory Block Pool (64 Blocks · 128 MB)
0x000000 (0.0 MB mark)Slack: 0 tokens (0.00 MB unconsumed)
Shared across multiple sequences
Immutable prompt hash
Sequence Block Tables (Logical L_i → Physical P_k Translation)
PagedAttention Memory Manager Event Stream
Prefix Caching & CoW
Load the Copilot (Prefix) preset. Observe how requests #1, #2, and #3 share identical system prompt blocks (P_0 and P_1) with a refCount = 3 badge, completely eliminating duplicate VRAM usage.
Block Size Trade-Offs (16 vs 32)
Toggle Block Sizebetween 16 and 32 tokens. At 16 tokens, internal fragmentation is minimized to < 3.5% tail slack. At 32 tokens, block table lookup overhead is halved, but average tail waste increases proportionally to $B / 2$.
Contiguous Waste vs Paged Concurrency
Examine the Memory Efficiency Meters. In contiguous allocation, 60%–85% of VRAM is wasted waiting for ungenerated tokens. PagedAttention eliminates external fragmentation completely, delivering a sustained 2.5×–4× concurrency gain.