# RuntimeError: 内存不足错误：无法为KV缓存块分配内存。请求块大小：16，空闲块：0

- **ID:** `llm/vllm-block-manager-out-of-memory`
- **领域:** llm
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

vLLM的PagedAttention块管理器因gpu_memory_utilization设置不足或并发请求超出内存预算，耗尽了KV缓存块的所有可用GPU内存。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| vLLM 0.3.0 | active | — | — |
| vLLM 0.4.0 | active | — | — |
| vLLM 0.4.2 | active | — | — |
| CUDA 12.1 | active | — | — |
| CUDA 12.2 | active | — | — |

## 解决方案

1. ```
   将gpu_memory_utilization降低到0.85或更低，并适当设置--max-num-seqs：

python -m vllm.entrypoints.openai.api_server --model meta-llama/Llama-2-7b-hf --gpu-memory-utilization 0.80 --max-num-seqs 64
   ```
2. ```
   通过--swap-space启用交换空间，在GPU内存紧张时将KV缓存卸载到CPU：

python -m vllm.entrypoints.openai.api_server --model meta-llama/Llama-2-7b-hf --swap-space 16
   ```

## 无效尝试

- **** — Larger batch tokens consume more KV cache memory without freeing space; the error persists or worsens. (60% 失败率)
- **** — Truncates all inputs, degrading model quality for long-context tasks; may still OOM if concurrent requests are high. (40% 失败率)
