Memory Architecture & Custom Allocators
Before we can understand GPU VRAM bottlenecks, KV-cache paging, or high-throughput LLM serving, we trace how operating systems allocate and address physical and virtual memory.
Day 1: Virtual Memory Layout & Struct Alignment
✓ CompletedMapped the 64-bit Linux process virtual address space (Text, Data, BSS, Heap, Stack) and measured the mechanical penalty of memory alignment and silent compiler struct padding.
Tensors in PyTorch/CUDA are contiguous blocks aligned to 256-byte or 512-byte boundaries for vectorized memory coalescing.
When activation tensors and KV-cache blocks fragment memory, an inference pipeline throws
"CUDA: Out of Memory" even when nvidia-smi shows gigabytes of total free VRAM.
Understanding allocator coalescence is the bedrock of GPU optimisation.
Day 2: Raw Syscalls & The Program Break: Bump Allocator (9.5 ns vs 169.7 ns glibc)
✓ Completed
Queried and expanded the Linux heap boundary via sbrk(), enforcing 16-byte bitwise alignment
to build an ultra-low-latency $O(1)$ Bump/Arena Allocator. Benchmarked 1,000,000 allocations against standard glibc malloc.
High-throughput LLM serving runtimes (vLLM, TensorRT-LLM) cannot call dynamic kernel allocators during active token generation. Intermediate activation tensors only live for the duration of a single forward pass layer. By using pre-allocated static scratchpad arenas, allocations complete in 9 nanoseconds and arena reset requires resetting an offset pointer (1 CPU cycle).
Day 3: In-Band Metadata Headers & Linked-List Free Lists (malloc / free)
✓ Completed
Bump allocators are fast but cannot free individual chunks. Built a general-purpose Free-List Allocator prepending
every chunk with a 16-byte aligned BlockHeader. Implemented First-Fit search, dynamic block splitting to kill internal fragmentation,
and immediate neighbor coalescing.
In autoregressive LLMs, caching Key-Value vectors traditionally wasted 60% to 80% of GPU VRAM due to rigid contiguous over-allocation. vLLM solved this through PagedAttention: managing GPU memory using a Free List of fixed 16-token physical blocks. By allocating blocks dynamically and returning freed blocks on request completion, VRAM waste plummeted under 4%, multiplying real-world serving throughput by 2x to 4x.
Upcoming Week 1 Sprints:
- Day 4: External Fragmentation & Boundary-Tag Bidirectional Coalescence
- Day 5: High-Throughput Microbenchmarks: Custom Allocator vs
glibc ptmalloc - Weekend Capstone: The Silicon-to-Synapse Architecture Synthesis Essay (Substack & Medium)