Model and Inference EngineeringKV cache and capacityStaff
Why can CUDA graph capture retain GPU memory while an inference server is idle?
By Amit Kumar3 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
After CUDA graph warmup, a server retains substantially more GPU memory even after all requests finish. Explain graph replay and stable addresses, distinguish graph pools from model weights and KV cache, and design startup accounting and capture-shape policies that balance capacity with tail latency.
Draft your approach first
Take a few minutes before you read the answer. Name the constraint that matters most, the failure you are designing for and how you would know it worked.
Reveal the worked answerWorked answer
A CUDA graph records a sequence of GPU operations so the runtime can launch that sequence with less repeated CPU overhead. Replaying it uses the recorded address relationships. Keeping those relationships valid can require memory to remain reserved even when no customer request is currently using the graph.
Its input and output buffers and the allocations tied to graph capture cannot simply be returned after each request like ordinary temporary tensors. PyTorch's CUDA graph documentation explains the long-lived addresses and private graph pools. vLLM's memory guidance also notes that CUDA graphs take extra GPU memory. So idle requests do not imply idle allocations. The process is keeping captured work ready for a future decode step.
In PyTorch 2.9, the graph-private pool remains alive until the graph object and tensors created during capture leave scope. The versioned memory-management section explains that lifetime. Clearing a request object or calling a general cache-release function does not prove the capture’s live storage is reclaimable. Conversely, a high reserved-memory counter alone does not prove a leak. Track which live object owns the pool and whether memory grows without bound when replaying already captured shapes.
For a hypothetical server, suppose weights account for 20 GiB, the configured attention cache, which stores prior tokens’ key/value states, for 10 GiB and required graph captures for another 2 GiB of live storage. Completing a request may release its cache blocks for reuse while leaving the captures’ 2 GiB intact. If the same captured shapes run repeatedly without further growth, those idle bytes can be intentional ownership. If each identical request retains another 100 MiB, investigate references and unexpected recapture. These illustrative buckets must be measured rather than added blindly if the implementation shares backing pools.
The amount depends on the engine, model, graph mode, captured batch or sequence shapes, and whether captures share a memory pool. A serving engine may prepare several shapes so common decode batches avoid Python and kernel-launch overhead. More graph variants can mean more persistent memory. I would record allocated and reserved memory before model load, after weights, after graph capture, and after representative traffic. Break out KV cache, graph pools and any compiled-kernel workspace. GPU process memory or nvidia-smi alone does not identify which owner is responsible.
For a controlled experiment, take snapshots after weights load, after each new capture and after repeated requests using only existing captures. Growth when adding a capture is different from growth on every identical replay. Pool sharing also needs correctness checks: graphs that overwrite the same storage must run under the supported ordering and concurrency rules. Selecting fewer capture sizes can reclaim capacity at the price of eager-execution latency for uncovered shapes. Keep an explicit fallback and measure its frequency under production-like batch shapes.
Suppose an operator sizes admission using free memory before graph capture. The first request later triggers capture and the engine has less room for KV than the plan assumed. Reverse the order: capture required shapes during startup, then derive a safe KV budget from the post-capture steady state. If graph memory is too large, reduce captured shape variants, use a more selective graph mode, share pools where the implementation supports it, or fall back to eager execution for rare shapes. Each change trades some latency for capacity. Test under actual batch composition because a graph configuration that helps batch one may not help all traffic.
The harder follow-up is why disabling graphs appears to “fix” OOM but worsens p99. Both results can be true. The correct comparison is completed throughput and first/inter-token latency at a fixed memory and load budget, with startup capture included in rollout capacity. CUDA graph replay is faster. Why does it return the wrong tokens for some batches? is about a graph replay producing wrong tokens for some batches. This page is about persistent memory ownership and how that changes admission even when replay is correct.
Related questions
Keep practising model and inference engineering
Follow another question in this area, or search the complete library.
Get my essays on AI engineering and distributed systems by email. Subscribe to ArchCrux