
How to Fix vLLM Out of Memory (OOM) and KV Cache Allocation on Linux
Quick Fix (TL;DR)
vLLM crashes with ValueError: The model's max seq len (32768) is larger than the maximum number of tokens that can be stored in KV cache or torch.cuda.OutOfMemoryError because it attempts to pre-allocate 90% of your total VRAM to the KV cache by default.
To fix the crash immediately on a single GPU (such as an RTX 3090, 4090, or A10G), start the server with explicit memory utilization and capped sequence length:
python3 -m vllm.entrypoints.openai.api_server \
--model Qwen/Qwen2.5-Coder-7B-Instruct \
--gpu-memory-utilization 0.85 \
--max-model-len 8192 \
--enforce-eager
VRAM Sizing & KV Cache Requirements Matrix
vLLM splits GPU memory into two distinct pools: Model Weights (static) and the PagedAttention KV Cache (dynamic request buffer). If model weights exceed the remaining headroom after pre-allocation, the process crashes during startup.
| Model Size | Precision | Base Model VRAM | Min KV Cache (8k Context) | Recommended GPU | Safe --max-model-len |
|---|---|---|---|---|---|
| 7B / 8B | FP16 / BF16 | 15.2 GB | 3.5 GB | RTX 3090 / 4090 (24GB) | 16,384 |
| 7B / 8B | AWQ / GPTQ (4-bit) | 5.8 GB | 2.2 GB | RTX 3060 12GB / 4070 12GB | 8,192 |
| 14B | AWQ (4-bit) | 9.4 GB | 3.8 GB | RTX 4080 16GB / A5000 | 8,192 |
| 32B | AWQ (4-bit) | 19.8 GB | 3.8 GB | RTX 4090 (24GB) | 4,096 |
| 70B | AWQ (4-bit) | 39.5 GB | 7.2 GB | 2x RTX 3090 (48GB Total) | 8,192 |
Testbed: Ubuntu 24.04 LTS, CUDA 12.4, PyTorch 2.4, vLLM 0.6.2.
Cause 1: Default KV Cache Pre-Allocation Conflicts
By default, vLLM sets --gpu-memory-utilization to 0.90 (90% of total VRAM). If the Linux desktop environment, X11, Wayland, or a background PyTorch process consumes even 1.5 GB of VRAM, the initialization fails.
Fix: Lower GPU Memory Utilization
Lower the allocation target to 0.80 or 0.85:
--gpu-memory-utilization 0.80
This reserves 20% of your GPU VRAM for CUDA context overhead and system display servers.
Cause 2: Unbounded Model Sequence Length
Many modern open models define a default context length of 32,768 or 131,072 tokens in their config.json. vLLM allocates KV cache blocks based on this maximum ceiling. On a 24GB card, allocating space for a 128k context requires more memory than the card physically possesses.
Fix: Restrict Context with --max-model-len
Cap the token context to match your actual application requirements:
# For coding assistants and refactoring workflows
--max-model-len 8192
# For short conversational agents and summarization
--max-model-len 4096
Cause 3: PyTorch CUDA Graphs Memory Spikes
vLLM captures CUDA graphs during server warmup to minimize kernel launch overhead and maximize batch inference throughput. However, graph capture causes temporary memory spikes that trigger torch.cuda.OutOfMemoryError on consumer cards running close to capacity.
Fix: Disable CUDA Graphs with --enforce-eager
Pass the --enforce-eager flag to execute PyTorch kernels in eager mode without pre-capturing CUDA graphs:
--enforce-eager
Note: Eager execution reduces inference throughput by approximately 5% to 10%, but enables models to run smoothly on cards that would otherwise crash during startup.
Cause 4: Out of Memory During High Concurrency
If your server starts up cleanly but crashes under simultaneous multi-user loads, the KV cache is running out of available pages for active streams.
Fix: Adjust Max Batched Tokens and Swap Space
Allocate CPU RAM as emergency paging space when the GPU KV cache saturates:
python3 -m vllm.entrypoints.openai.api_server \
--model Qwen/Qwen2.5-Coder-7B-Instruct \
--gpu-memory-utilization 0.85 \
--max-model-len 8192 \
--swap-space 8 \
--max-num-seqs 16
--swap-space 8: Allocates 8 GB of host system RAM to offload inactive request contexts.--max-num-seqs 16: Limits concurrent active generation streams to 16, queueing subsequent requests safely.
Verification: Test Throughput with Client Query
Send a quick test inference request using curl to ensure the server processes tokens without memory errors:
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen2.5-Coder-7B-Instruct",
"messages": [
{"role": "user", "content": "Write a Python function to check for prime numbers."}
],
"max_tokens": 128
}'
Inspect nvidia-smi during generation. Memory utilization should remain stable without spikes or unexpected kernel terminations.