# Fix torch.OutOfMemoryError: Allocation on device 0 would exceed allowed memory in ComfyUI (FLUX & Wan 2.1)

> **Direct Error Triage**: ComfyUI raises torch.OutOfMemoryError: Allocation on device 0 would exceed allowed memory when generating images or video with FLUX.1 [dev], Wan 2.1 14B, or HunyuanVideo because the text encoder (T5-XXL / UMT5-XXL), Diffusion Transformer (DiT), and 3D/2D VAE decode activations exceed physical VRAM or collide with Windows WDDM shared memory. Fix it by launching ComfyUI with --reserve-vram 1.5, switching to GGUF Q4_K_M or fp8_e4m3fn checkpoints, and replacing VAE Decode with VAE Decode (Tiled).

## Error Signature
```
torch.OutOfMemoryError: Allocation on device 0 would exceed allowed memory
```

## Root Cause & Memory Allocation Mechanics
Modern flow-matching Diffusion Transformers have three distinct memory phases inside a ComfyUI execution graph: (1) Text Conditioning (`T5-XXL` at `9.8 GB` FP16 or `UMT5-XXL` at `11.4 GB` FP16), (2) Iterative Latent Denoising (`12B–14B DiT` weights plus cross-attention activation maps), and (3) Spatial/Temporal VAE Decoding. On Windows 11, if ComfyUI's smart memory manager miscalculates free VRAM because Desktop Window Manager (DWM) or a web browser is holding 1.2 GB, either Windows silently spills tensors into slow shared system RAM (dropping speed by 10x) or `--cuda-malloc` throws `Allocation on device 0 would exceed allowed memory` during the final VAE upscaling convolution.

## Step-by-Step CLI Remediation
### Ollama
```bash
# Unload any background Ollama LLMs stealing VRAM before running ComfyUI!
ollama stop $(ollama ps | awk 'NR>1 {print $1}')
```

### Llama.cpp
```bash
# Launch ComfyUI with explicit VRAM reserve and PyTorch expandable segments
export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True
python main.py --reserve-vram 1.5 --use-sage-attention

# For 8GB–12GB GPUs, force lowvram weight streaming:
python main.py --lowvram --reserve-vram 1.0
```

### vLLM
```bash
# In ComfyUI workflow nodes:
# 1. Use 'Load Diffusion Model (weight_dtype: fp8_e4m3fn)' or 'Unet Loader (GGUF)'
# 2. Load 't5xxl_fp8_e4m3fn.safetensors' in DualCLIPLoader
# 3. Replace 'VAE Decode' with 'VAE Decode (Tiled)' (tile_size=256 or 512)
```

## Verified Hardware & Cloud Pathways
- Explore 24GB GPUs on Amazon: https://www.amazon.com/s?k=RTX+4090+24GB+graphics+card&tag=sparecables-20 (rel="noopener sponsored")
- Rent on RunPod: https://runpod.io?ref=ynypzu94 (rel="noopener sponsored")
- Rent on Vast.ai: https://cloud.vast.ai/?ref_id=723130 (rel="noopener sponsored")

---
*Interactive Sizing Calculator available at: https://www.gpugarden.com/oom/comfyui-allocation-on-device/*
